简体   繁体   中英

Powershell [ Invoke-WebRequest] is it posible to make copy of Session from a certain point and then re-use session from the saved state?

Powershell [ Invoke-WebRequest] is it posible to make copy of Session from a certain point and then re-use session from the saved state?

I have no problem logging in and completing the full request.

The question is that I should perform hundreds of similar requests and to make a selection, for each parameter I send a separate post request (there are eight of post request) and I have to execute these requests sequentially.

the first four are identical for everyone, so, is it possible to save state of session after these four requests in copy of variable and then use it?

i tried to make copy of session using $sessionAfterBasicRequest=$session.PSObject.Copy() but it's not working

maybe I'm missing basic things...

I'm having trouble understanding what you're trying to do, but I think you want to maintain a single session for these many requests.

See this example for Invoke-WebRequest , explaining how to maintain the session foir a stateful web service when using `Invoke-WebRequest. I'll repeat the content below for posterity, since this example already explains what is going on:

$Body = @{
    User = 'jdoe'
    password = 'P@S$w0rd!'
}
$LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST'

$Session

$ProfileResponse = Invoke-WebRequest 'https://www.contoso.com/profile/' -WebSession $Session

$ProfileResponse

The first call to Invoke-WebRequest sends a sign-in request. The command specifies a value of "Session" for the value of the -SessionVariable parameter, and saves the result in the $LoginResponse variable. When the command completes, the $LoginResponse variable contains an BasicHtmlWebResponseObject and the $Session variable contains a WebRequestSession object. This logs the user into the site.

The call to $Session by itself shows the WebRequestSession object in the variable.

The second call to Invoke-WebRequest fetches the user's profile which requires that the user be logged into the site. The session data stored in the $Session variable is used to provide session cookies to the site created during the login. The result is saved in the $ProfileResponse variable.

The call to $ProfileResponse by itself shows the BasicHtmlWebResponseObject in the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM