PowerShell 6.2.1 doesn't return a Forms property using Invoke-WebRequest. The way I used to do it in 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
I see in the Microsoft documentation that there is now a -Form
parameter you can use, but I've been hacking at it for a few hours now with very little success.
They provided two possible solutions.
Example 1
$Uri = 'https://api.contoso.com/v2/profile'
$Form = @{
firstName = 'John'
lastName = 'Doe'
email = '[email protected]'
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
Example 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
I have tried both and my current code block is a combination of the two and it gets me at least a good failure.
$username = '[email protected]'
$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
This is the error I am now getting.
Invoke-WebRequest :
403 Forbidden
Forbidden You don't have permission to access /login on this server.
At line:7 char: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
This is actually the best response I've got.