Powershell invoke-webrequest for website login
I've had a lot of success using invoke-webrequest to log into websites, but I'm stumped. I am trying to log into https:// ctslink.com
or https:// direct.ctslink.com
. The login form has a hidden token field that changes every time I try to login, I believe this is causing the problem. Another thing I noticed is that the session variable $fb
is null after the first invoke-webrequest call.
$r=Invoke-WebRequest www.ctslink.com -SessionVariable $fb
$form = $r.Forms[0]
$form.Fields["userId"] = "MyUsername"
$form.Fields["passwd"] = "MyPassword"
$r=Invoke-WebRequest 'https://ctslink.com/login.do' -WebSession $fb -Body $form.Fields
Any help would be greatly appreciated, Mike
+3
source to share
1 answer
Don't put the $ variable argument for the session variable argument. Try it -
$c = $host.UI.PromptForCredential('Your Credentials', 'Enter Credentials', '', '')
$r = Invoke-WebRequest 'http://1.2.3.4/' -SessionVariable my_session
$form = $r.Forms[0]
$form.fields['username'] = $c.UserName
$form.fields['password'] = $c.GetNetworkCredential().Password
$r = Invoke-WebRequest -Uri ('http://1.2.3.4' + $form.Action) -WebSession $my_session -Method POST -Body $form.Fields
+5
source to share