Using Powershell's Invoke-WebRequest to persist state between requests using SessionVariable

Recently, I've been testing ASP.NET MVC's Output Cache feature and wanted to test using different parameters such as VaryByCustom which would read a user's cookies and construct a cache key according to some value.

I ended up having to log in and out of Google Chrome, Firefox, Edge etc just to test this so I created a script to do so instead.

$loginUrl = ""
$normalUrl = ""

$postParams = @{email='';password='1234'}
iwr -Uri $url  -Method POST -Body $postParams -SessionVariable MySessionVariable
iwr $normalUrl -WebSession $MySessionVariable

# Optionally view what cookies got stored

The magic lies in the MySessionVariable variable which is basically a Cookie object that gets passed in each invocation.

comments powered by Disqus