I need to download a file using a POST request, however, since PowerShell seemingly can't handle binary data in pipes and variables (Out-File doesn't support binary data), I need to use the -OutFile parameter of Invoke-WebRequest. When doing this, however, it no longer outputs the status code so that I can capture it in a variable:
PS K:\hmpf> $Filepath = "E:\shmimpf\g\yyv.fgr"
PS K:\hmpf> $URL = "https://wwwh.ipswich.org/tycobrahe/magazine.php?burgerflipcount=76"
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post).StatusCode
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -Method Post).StatusCode
200
PS K:\hmpf>
From the documentation for Invoke-WebRequest:
-PassThru Indicates that the cmdlet returns the results, in addition to writing them to a file. This parameter is valid only when the OutFile parameter is also used in the command.
So to get the response results in conjunction with -OutFile:
(Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post -PassThru).StatusCode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With