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

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of Max Kozlov Max Kozlov 4 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #40559
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    PSv5.0

    I have this scenario:
    I go to http://somepage.com/login
    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 = 'http://somepage.com'
    $r1 = Invoke-WebRequest -Uri ($siteurl+'/login') -SessionVariable mysession
    $r1.Forms[0].Fields['Login']='admin'
    $r1.Forms[0].Fields['Password']='password'
    $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 🙂

    #40896
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    for those who are interested, a quick and dirty workaround

    not well tested but suitable for my needs

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.