15

Windows 8 にアップグレードしてから、目に見えない IE の起動に依存する多くの PowerShell スクリプトが機能しなくなったため、Invoke-WebRequestコマンドに切り替えてみました。私は多くのグーグルを行いましたが、それでもスクリプトを機能させることができません。

これがすべきことです:

  1. シンプルなフォーム (ユーザー名、パスワード、送信ボタン) で Web サイトをロードします。
  2. 資格情報を入力してください
  3. そしてそれらを提出してください。

Microsoft tech-net の例はあまり役に立ちませんでした。

$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

2番目のものは、$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 に大きく依存しています。

4

4 に答える 4

15

投稿を直接実行してみてください。例:

$formFields = @{username='john doe';password='123'}
Invoke-WebRequest -Uri $myUrl -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"
于 2012-12-05T21:17:38.307 に答える
7

署名されていない/信頼されていない証明書の問題に対処するには、次の行を追加します

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Invoke-WebRequest ステートメントの前

于 2014-09-08T19:47:03.307 に答える
3

質問の例は機能しますが、最初の行ではrbなく使用する必要があります。$rb

$response = Invoke-WebRequest -Uri $myUrl -Method Default -SessionVariable rb

($myUrl + '/login')これは私のログインアドレスであるため、私も使用する必要がありました。

$response = Invoke-WebRequest -Uri ($myUrl + '/login') -メソッドのデフォルト -SessionVariable rb

そして、使用された最後の行で($myUrl + $form.Action)

$response = Invoke-WebRequest -Uri ($myUrl + $form.Action) -WebSession $rb -Method POST
于 2016-06-09T15:00:50.453 に答える