powershell invoke-webrequest登录到网站 [英] powershell invoke-webrequest to log into website

查看:174
本文介绍了powershell invoke-webrequest登录到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用invoke-webrequest登录网站取得了很大的成功,但我很难过。我正在尝试登录 https:// ctslink.com https:// direct.ctslink.com 。登录表单有一个隐藏的令牌字段,每当我尝试登录时都会更改,我相信是导致问题的原因。我注意到的另一件事是在首次调用invoke-webrequest之后,会话变量 $ fb 为空。



  $ r =调用-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。领域

任何帮助将不胜感激,
Mike

解决方案

不要为会话变量参数放置$。试试这个 -

  $ 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 [''用户名'] = $ c.UserName
$ form.fields ['password'] = $ c.GetNetworkCredential()。密码
$ r = Invoke-WebRequest -Uri('http://1.2。 3.4'+ $ form.Action)-WebSession $ my_session -Method POST -Body $ form.Fields


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆