PowerShell v3 Invoke-WebRequest:表单问题 [英] PowerShell v3 Invoke-WebRequest: Troubles with forms

查看:43
本文介绍了PowerShell v3 Invoke-WebRequest:表单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我升级到 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.

这是它应该做的:

  1. 使用简单的表单(用户名、密码、提交按钮)加载网站,
  2. 输入凭据
  3. 并提交它们.

微软技术网示例对我,这就是我拼凑出来的:

The Microsoft tech-net examples were not very helpful for me, that is what I pieced 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"]     = "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 : 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.

推荐答案

尝试直接发帖,例如:

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

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