首页 > 解决方案 > 使用 powershell 在 Azure 自动化帐户中设置 API 版本

问题描述

我在 powershell azure 自动化运行手册中使用了以下消耗使用详细信息。

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

相同的命令和运行手册在另一个订阅中运行良好。但是在其中一个订阅中,它给出了以下错误

当前 api 版本不支持订阅范围使用。请使用 2019-10-01 之后的 api 版本

如何在 Azure Powershell 中设置 apiversion?

标签: azurepowershell

解决方案


Get-AzConsumptionUsageDetail没有ApiVersion参数。

我的解决方法是使用下面的命令。首先,使用您的登录帐户获取令牌,然后请求API以获取使用详细信息。错误提到的api-version是2019-10-01。

# 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}应该代替与使用细节操作相关的范围。您可以参考文档以获取更多详细信息。


推荐阅读