自从我升级到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.
我再次严重依赖于Microsoft的示例#2。
无法建立安全连接的 SSL/TLS 信任关系。
... - Phil Strahl