0

I'm trying to create a 'GET' method request to see if an old Azure Cloud Service (classic) is available or not - send a request and get the status code to see if its 200 OK or not.

I tried the following code:

function Get-AzCachedAccessToken() {
    $AzureContext = Get-AzContext
    $CurrentAzureProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;
    $CurrentAzureProfileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($CurrentAzureProfile);
    $AzureAccessToken = $CurrentAzureProfileClient.AcquireAccessToken($AzureContext.Tenant.Id).AccessToken;
    $AzureAccessToken
}

function Get-AzBearerToken() {
    $ErrorActionPreference = 'Stop'
    ('Bearer {0}' -f (Get-AzCachedAccessToken))
}

$CloudServiceURL = "http://test-myapp.cloudapp.net"

Invoke-RestMethod $CloudServiceURL `
-Headers @{Authorization = (Get-AzBearerToken) } `
-Method Get

The error I get is: error msg

I'm also using an old Azure module in an automation workflow such as this: PowerShell Workflow Runbook so I could run it.

Is there anyway to restart an Azure classic cloud service role every interval?

Adela
  • 23
  • 4
  • try ```Invoke-RestMethod $($CloudServiceURL)``` also do you have a random ` in your code or is it me ? – dcaz Dec 09 '21 at 19:40
  • @dcaz I don't understand your question – Adela Dec 12 '21 at 10:19
  • The line ```-Headers @{Authorization = (Get-AzBearerToken) } ` ``` <------has a random backtick ` in it. Does your code have that? – dcaz Dec 12 '21 at 18:08
  • Its not random, its a line break before the '-method get' – Adela Dec 14 '21 at 09:00
  • 1
    Why are you passing an authorization header? And if your Cloud Service website really does need an auth header, then you are passing the wrong one since the one you are using is your ARM auth header. Also, what does the detailed error response look like? (see https://stackoverflow.com/questions/29613572/error-handling-for-invoke-restmethod-powershell) – kwill Dec 26 '21 at 06:47

0 Answers0