Invoke-RestMethod authorization problem

Product: PowerShell Universal
Version: 4.0.2


I can run this script from a powershell window with no problem:

Invoke-RestMethod -Authentication “Basic” -Credential $cred -OutFile “”

but when I add it to a PSU script like this:

$response = Invoke-RestMethod -Authentication “Basic” -Credential $secret:ricloud -OutFile “”

I get this error:

[error] {“httpStatusCode”:401,“message”:“Insufficient Authorization”,“details”:“The Authorization HTTP Header is missing or malformed”}

I know $secret:ricloud is correct, because I use it in several endpoint scripts. Any idea why I am getting this error?


Ok, this is strange. I can get it to work with a Post method. The file is downloaded ok, but I do get a 405 error as the endpoint I am using doesn’t like Post methods. For now I can do this, but I would like to know why the Get method does not work

You could try to use cookie auth directly. I’m not sure why what you are trying isn’t working.

        Invoke-RestMethod -Method Post -Body (@{
                Username = $secret:ricloud.UserName
                Password = $secret:ricloud.GetNetworkCredential().Password
            } | ConvertTo-Json) -SessionVariable Session -ContentType "application/json"

$response = Invoke-RestMethod -WebSession $Session -OutFile “”


Figured this out. The powershell window was caching a different set of credentials. My bad.



1 Like