Shiv
Shiv

Reputation: 31

Setting Api version in Azure automation account using powershell

I was using the following consumption usage detail in powershell azure automation runbook.

Get-AzureRmConsumptionUsageDetail -StartDate $startDate -EndDate $endDate -ResourceGroup

The same command and runbook works fine in another subscription. But in one of the subscription it gives the following error

Subscription scope usage is not supported for current api version. Please use api version after 2019-10-01

How to set apiversion in Azure Powershell ?

Upvotes: 3

Views: 1517

Answers (1)

unknown
unknown

Reputation: 7483

Get-AzConsumptionUsageDetail do not have ApiVersion parameter.

My workaround is to use the command below. First, get the token with your login account, and then request the API to get the usage details. The api-version is 2019-10-01 what the error mentioned.

# login
Connect-AzAccount

# get accessToken
$resource = "https://management.azure.com"
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$accessToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken

#request REST API
$uri = "https://management.azure.com/{scope}/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01"
Invoke-RestMethod -Method 'Get' -Uri $uri -Headers @{ Authorization = "Bearer " + $accessToken }

{scope} should be instead of the scope associated with usage details operations. You could refer to the doc for more details.

Upvotes: 2

Related Questions