我在 powershell azure 自动化 Runbook 中使用了以下消耗使用详细信息。
Get-AzureRmConsumptionUsageDetail -StartDate $startDate -EndDate $endDate -ResourceGroup
相同的命令和 Runbook 在另一个订阅中运行良好。 但在其中一个订阅中,它给出了以下错误
Subscription scope usage is not supported for current api version. Please use api version after 2019-10-01
如何在 Azure Powershell 中设置 apiversion?
最佳答案
Get-AzConsumUsageDetail
没有 ApiVersion
参数。
我的解决方法是使用下面的命令。首先,使用您的登录帐户获取 token ,然后请求 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}
应该代替与使用详细信息操作关联的范围。您可以引用doc了解更多详情。
关于azure - 使用 powershell 在 Azure 自动化帐户中设置 Api 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62837947/