【发布时间】:2012-12-05 17:55:28
【问题描述】:
自从我升级到 Windows 8 后,我的许多依赖于启动不可见 IE 的 PowerShell 脚本将不再工作,因此我尝试切换到 Invoke-WebRequest 命令。我做了很多谷歌搜索,但仍然无法让我的脚本正常工作。
这是它应该做的:
- 使用简单的表单(用户名、密码、提交按钮)加载网站,
- 输入凭据
- 并提交。
Microsoft tech-net examples 对我没有多大帮助,这是我拼凑起来的:
$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 字段时:
Cannot index into a null array.
$form.Fields["user"] = "username"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
第二个与$form.Action 有关,我不知道它应该读什么:
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.
再次,我非常依赖example #2 at Microsoft。
【问题讨论】:
标签: forms powershell web webrequest