PowerShell v3 Invoke-WebRequest:与表单混淆 [英] PowerShell v3 Invoke-WebRequest: Troubles with forms
问题描述
由于我升级到Windows 8,很多依赖启动隐形IE的PowerShell脚本不再适用,所以我尝试切换到 Invoke-WebRequest 命令。我做了大量的谷歌搜索,但仍然无法让我的脚本工作。
Since I upgraded to Windows 8 a lot of my PowerShell scripts relying on launching an invisible IE won’t quite work anymore, so I tried switching to the Invoke-WebRequest command. I did a lot of googling but still can’t get my script to work.
这是它应该做的:
- 用简单的表单(用户名,密码,提交按钮)加载网站,
- 输入凭证
- 并提交。
借助令人难以置信的微软技术网示例的帮助(至少对我而言) ),我拼凑在一起:
With the "help" of the incredibly bad Microsoft tech-net examples (at least for me), I pieced the following together:
$myUrl = "http://some.url"
$response = Invoke-WebRequest -Uri $myUrl -Method Default -SessionVariable $rb
$form = $response.Forms[0]
$form.Fields["user"] = "username"
$form.Fields["password"] = "password"
$response = Invoke-WebRequest -Uri $form.Action -WebSession $rb -Method POST
$response.StatusDescriptionOK
我收到两个错误,第一个错误是尝试写入user字段:
I receive two errors, the first one when trying to write into the "user" field:
无法索引到一个空数组中。
Cannot index into a null array.
$ form.Fields [user] =用户名
$form.Fields["user"] = "username"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
第二个与 $ form.Action
有关,我不知道应该读什么:
The second one has to do with the $form.Action
which I have no idea what it should read:
Invoke-WebRequest:无法验证参数'Uri'的参数。参数是
null或空。提供一个非null或空的参数,然后再次尝试命令
。
Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
同样,我非常依赖< 微软示例2)。
编辑:感谢哈马尔的大写。我的礼貌在哪里? ;)
Thanks to hammar for the capitalization. Where are my manners? ;)
推荐答案
尝试直接发布帖子,例如:
Try doing the post directly e.g.:
$formFields = @{username='john doe';password='123'}
Invoke-WebRequest -Uri $myUrl -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"
这篇关于PowerShell v3 Invoke-WebRequest:与表单混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!