powershell invoke-webrequest 登录网站 [英] powershell invoke-webrequest to log into website
问题描述
我使用 invoke-webrequest 登录网站取得了很多成功,但我很困惑.我正在尝试登录 https://ctslink.com
或 https://direct.ctslink.com
.登录表单有一个隐藏的令牌字段,每次我尝试登录时都会更改,我相信这是导致问题的原因.我注意到的另一件事是会话变量 $fb
在第一次调用 invoke-webrequest 后为空.
I have been having a lot of success using invoke-webrequest to log in to 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 which changes every time I try to login, I believe is what is causing the problem. Another thing I noticed was the session variable $fb
is null after the first call to invoke-webrequest.
$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
推荐答案
不要为会话变量参数添加 $.试试这个 -
Don't put a $ for the session variable argument. Try this -
$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
这篇关于powershell invoke-webrequest 登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!