Authentication failed. The ‘Authorization’ header is missing – Invoke-WebRequest Azure ARM

While building an automation procedure I had to POST the management API of Azure.

Invoke-WebRequest -Uri https://management.azure.com/subscriptions/pool/xxx/xxxx/xxxx/xxxx -Method POST

When I used the Invoke-WebRequest without Authentication it failed. The error that you will get when you do not have Authentication headers set can be found below.

Invoke-WebRequest: {"error":{"code":"AuthenticationFailed","message":"Authentication failed. The 'Authorization' header is missing."}}

In order to authenticate with azure you can get an access-token using az cli.

az account get-access-token

The output will look like the below output:

{
  "accessToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxVG-hTDHECYJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-B5FA9l9RvqOls3iaDDYw5O86acvLIwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-ixxxxxxxxxxxxxxxxx3eoeKlxZfxxxxxxxxxxfGW2O6oA",
  "expiresOn": "2022-05-05 14:51:08.000000",
  "subscription": "a23eef11-f200-4722-866c-248ca45142f6",
  "tenant": "1efa646f-3fc3-4554-bd8c-651879a2b110",
  "tokenType": "Bearer"
}

You should take the output of the token and use it on your call.

For example using POSTMAN you should place your token under the Authorization tab. You will then be able to get the result from Azure ARM api.

Powershell handle Invoke-WebRequest 404 error

In some cases you may need to use Invoke-WebRequest of powershell as a correct result with status code of 404. A use case for this scenario would be a smoke test for a particular service or URL. In newer powershell versions you can use SkipHttpErrorCheck in order to stop powershell from failing the script.

The below example is a simple Web request that will fail on Powershell <7 with an exception if a 404 response is returned from the webpage.

$req = Invoke-WebRequest -Uri $url -Method GET

The parameter that you could use with Powershell 7++ and you would not have the powershell task failed would be.

$req = Invoke-WebRequest -Uri $url -Method GET -SkipHttpErrorCheck

In order to bypass this issue you could handle the exception on Powershell <7 and add your logic inside the catch block. For example

try {
    $resultApi = MakeWebRequest("http://localhost:1234/api/console") 
}
catch {
    if( $_.Exception.Response.StatusCode.Value__ -eq 404 ) 
    {
        Write-Host "API console Test 404 , proceeding ..."
    }
    else {
        Write-Host "False response..."
    }
}