Invoke-WebRequest doesn't update session variable in mid-request ?


I have this scenario:
I go to
the page set cookie and display login form
I enter credentials, the page accept it,set some more cookies and redirect me (302) to main page.
I try to implement this scenario in powershell with Invoke-WebRequest

$siteurl = ''
$r1 = Invoke-WebRequest -Uri ($siteurl+'/login') -SessionVariable mysession
$r2 = Invoke-WebRequest -Uri ($siteurl+$r1.forms[0].Action) - WebSession $mysession -Method Post -Body $r1.Forms[0].Fields

…and I can’t login because while I get redirected within $r2 request
site set some more cookies but when redirected doesn’t use these cookies!
NetCapture looks like

request:  GET /Login
response: 200 OK
response: Set-Cookie Cookie1

request:  GET /Login-Proceed (Action url)
request:  Cookie: Cookie1
response: 302, Location /Main
response: Set-Cookie Cookie2

request:  GET /Main
request:  Cookie: Cookie1
!!!!!! request:  Cookie: Cookie2 missed here !!!!!

IE set both cookies on this stage

Anyone already meet these problems ? Any thoughts, workarounds ?

(yes, i know about COM IE object, System.Net.WebClient and so on, but I hoped to use the generic way :slight_smile:

for those who are interested, a quick and dirty workaround

not well tested but suitable for my needs