azure - 使用 powershell 在 Azure 自动化帐户中设置 Api 版本

标签 azure powershell

我在 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/

相关文章:

powershell - 显示进程消耗内存和CPU时间

Powershell显示特定文件夹名称的递归内容

AzureADB2C.UI - 登录后将用户重定向到页面

azure - 使用 Azure AD 的 Spring Boot+Oauth 客户端凭据授予流程

Azure 认知搜索忽略字段

windows - 将 PowerShell 转换为 Bash

windows - 从 powershell 触发任务调度程序作业

powershell - 将if语句与管道命令一起使用

sql - 如何首先使用代码在 Sql Azure .NET 中添加一对多关系?

c# - 序列化 XML 并保存到 Azure、C#