4

PowerShell 6.2.1 は、Invoke-WebRequest を使用して Forms プロパティを返しません。Powershell 5で行っていた方法:

$r = Invoke-WebRequest -Uri 'somewebsite.com' -SessionVariable f
$f
$form = $r.Forms[0]

$form.Fields["userName"] = "username"
$form.Fields["brokerPassword"] = "password"

$login = Invoke-WebRequest -Uri 'somewebsite.com' -WebSession $f -Method 
POST -Body $form.Fields

Microsoft のドキュメントには、-Form使用できるパラメーターがあることがわかりますが、数時間ハッキングしてきましたが、ほとんど成功していません。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6

彼らは2つの可能な解決策を提供しました。

例 1

$Uri = 'https://api.contoso.com/v2/profile'
$Form = @{
firstName  = 'John'
lastName   = 'Doe'
email      = 'john.doe@contoso.com'
avatar     = Get-Item -Path 'c:\Pictures\jdoe.png'
birthday   = '1980-10-15'
hobbies    = 'Hiking','Fishing','Jogging'
}
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form

例 2

$Body = @{
User = 'jdoe'
password = 'P@S$w0rd!'
}
$LoginResponse = Invoke-WebRequest 'http://www.contoso.com/login/' - 
SessionVariable 'Session' -Body $Body -Method 'POST'

$Session

$ProfileResponse = Invoke-WebRequest 'http://www.contoso.com/profile/' - 
WebSession $Session

$ProfileResponse

私は両方を試しましたが、現在のコード ブロックは 2 つの組み合わせであり、少なくとも十分な失敗が得られます。

$username = 'useremail@gmail.com'
$password = 'p@ssw0rd'
$form = @{    
LoginForm_password = $password
LoginForm_username = $username
}
$result = Invoke-WebRequest 'https://www.podbean.com/login' - 
SessionVariable foo -Form $form -Method POST
$foo

$result | Get-Member | Format-table

これは私が今得ているエラーです。

Invoke-WebRequest :

403禁止します

禁止 このサーバーの /login にアクセスする権限がありません。

行:7 文字:11

  • $result = Invoke-WebRequest 'https://www.podbean.com/login' -SessionV ...
  •       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  • CategoryInfo : InvalidOperation: (Method: POST, Reque\u2026tent-Length: 357 }:HttpRequestMessage) [Invoke-WebRequest], HttpResponseException
  • FullyQualifiedErrorId : WebCmdletWebResponseException、Microsoft.PowerShell.Commands.InvokeWebRequestCommand

これは実際に私が得た最高の応答です。

4

1 に答える 1