Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 584
Shiv Avatar asked Dec 18 '25 11:12

Shiv


1 Answers

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.

like image 55
unknown Avatar answered Dec 20 '25 09:12

unknown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!