【问题标题】:Azure consumption api returning blank valueAzure消费api返回空白值
【发布时间】:2022-10-02 09:50:03
【问题描述】:

我正在尝试使用 azure 消费 api 来获取我的订阅的使用详细信息,但它一直返回一个空白数组。我知道那里应该有东西。

$VaultName = 
$KeyName = 

$tenantId = 
$applicationId = 
$SubscriptionId =

$apiEndpointUri = \"https://management.azure.com/\"  
$secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name $KeyName -AsPlainText

$RequestAccessTokenUri = \"https://login.microsoftonline.com/$tenantId/oauth2/token\"
$body = \"grant_type=client_credentials&client_id=$($applicationId)&client_secret=$($secret)&resource=$($apiEndpointUri)\" 

$AccessToken = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType $contentType

$apiuri = \"https://management.azure.com/subscriptions/$($SubscriptionId)/providers/Microsoft.Consumption/usageDetails?$filter={reportedstartTime=2022-03-01&reportedendTime=2022-04-01}&api-version=2021-10-01\"
$headers = New-Object \"System.Collections.Generic.Dictionary[[String],[String]]\"
$headers.Add(\"Authorization\", \"Bearer $($AccessToken.access_token)\")

Invoke-RestMethod -Uri $apiuri -Headers $headers | ConvertTo-Json

每次都是这样的回应

{
    \"value\":  [

              ]
}

我已经尝试了所有我能想到的改变 $apiuri 的方法。而且我知道所有的身份验证部分都是正确的。

    标签: azure api billing consumption


    【解决方案1】:

    您的筛选器中有一个名为“reportedstartTime”的属性,该属性不存在(它似乎与 Azure Stack 相关?)。根据Usage Details - REST API (Azure Consumption),可用属性是日期或 billingPeriodStartDate 和 billingPeriodEndDate。

    您使用的 URI 不应包含大括号。假设您没有使用 Azure 堆栈,那么您的 URI 应该类似于以下内容:

    https://management.azure.com/subscriptions/$($SubscriptionId)/providers/Microsoft.Consumption/usageDetails?$filter=properties/date+ge+2022-03-01+AND+properties/date+lt+2022-04-01&api-version=2021-10-01
    

    这会过滤给定订阅的数据,其中日期大于等于 4 月 1 日,小于 5 月 1 日。

    或者,如果您在特定月份之后,您可以指定一个计费周期而不是过滤 yyyy-mm-dd:

    https://management.azure.com/subscriptions/$($SubscriptionId)/providers/Microsoft.Billing/billingPeriods/202204/providers/Microsoft.Consumption/usageDetails?&api-version=2021-10-01
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多