I’m trying to write a PowerShell script that will maintain a session with a secure web page using Invoke-WebRequest. I’m running into a problem. The 2012 server I will have to run the script from sends a pup-up to the powershell host screen. This script will normally run with no logged on user when it goes into production.
I need to either A: Change the Windows settings that’s causing the cookie popup or B: have a way to handle it from within the script.
Is the -Force parameter going to force it to accept the cookie. Or is there any paramter that is used to handle this? I have no idea what Windows setting is causing that pop-up.
Well, you can accept session cookies and use them in future calls to Invoke-WebRequest via the -SessionVariable and -WebSession parameters, but I’m not sure that’s what you need. I haven’t seen a pop-up box related to cookies when using Invoke-WebRequest before.
# Set up a session variable on the first call to Invoke-WebRequest
Invoke-WebRequest @otherParams -SessionVariable myWebSession
# Use the session in future calls
Invoke-WebRequest @otherParams -WebSession $myWebSession
Do you know of a way to have it accept a self signed certificate of the site from within PowerShell?
The page also uses a session cookie and I keep getting this error. . .
OK so I still need help with this. I have a web page that basically looks like this. . .
[form box for user ID ]
[form box for password]
[button “login”]
I want to use the post method to put the user id and password in the 2 form boxes and I need to figure out how to tell PowerShell to click the button to submit the login credentials. If I capture the HtmlWebResponseObject objects returned from a web request…
There’s example code for just such a scenario in the Invoke-WebRequest help file:
# Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the results in the $r variable.
$r=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb
# Use the session variable that you created in Example 1. Output displays values for Headers, Cookies, Credentials, etc.
$fb
# Gets the first form in the Forms property of the HTTP response object in the $r variable, and saves it in the $form variable.
$form = $r.Forms[0]
# Pipes the form properties that are stored in the $forms variable into the Format-List cmdlet, to display those properties in a list.
$form | Format-List
# Displays the keys and values in the hash table (dictionary) object in the Fields property of the form.
$form.fields
# The next two commands populate the values of the "email" and "pass" keys of the hash table in the Fields property of the form. Of course, you can replace the email and password with values that you want to use.
$form.Fields["email"] = "User01@Fabrikam.com"
$form.Fields["pass"] = "P@ssw0rd"
# The final command uses the Invoke-WebRequest cmdlet to sign in to the Facebook web service.
$r=Invoke-WebRequest -Uri ("https://www.facebook.com" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields
You’ll need to modify it slightly to match your webpage, but it should hopefully get the job done.
Yep I tried using that example exactly and changed the URI value to mydomain but the login fields are different from facebook. I had already tried that I was hoping some one with experience getting it working could look at the output I posted and help me discover how to use it to login to my app.
His is example is on a PHP page this page is heavy on JAVA and JSON. I know PowerShell can handle JSON but I am having trouble ramping up on making this work.
It’s hard to give you exact code without being able to connect to the website myself. If it’s just the field names that are giving you trouble, try changing these lines in the example:
$form.Fields["email"] = "User01@Fabrikam.com"
$form.Fields["pass"] = "P@ssw0rd"
# Try changing them to this:
$form.Fields["core_userid"] = "User01@Fabrikam.com"
$form.Fields["core_password"] = "P@ssw0rd"