0

我正在尝试使用 Powershell 获取订阅数据的 Azure 计费数据。

主要从 MSDN https://docs.microsoft.com/ja-jp/rest/api/consumption/usagedetails/list检查文档

和一个样本如下。 https://www.cloudnative.at/2017/12/22/generate-an-azure-consumption-report-with-the-consumption-rest-api-and-powershell/

$loginUri = "https://login.microsoft.com"
$body =@{
    client_id = XXXX
    client_secrect = XXXXXXXX
    resource    =  "https://management.core.windows.net"
    grant_type = "client_credentials"
}

$oauth = Invoke-RestMethod -Method Post -Uri $loginUrl/$TenantID/oauth2/token?api-version=1.0 -Body $body 

# SubscriptionID and Billing Period
$SubscriptionId = '<Your subscription GUID here>'
$billingperiod = '202006-1'

#Create the REST-URL
$usageURL =     "https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"

成功获得身份验证令牌后,运行请求 uri 时出错

“AuthenticationFailed”,对象 ID 为“XXXX”的客户端“XXXXXX”无权在范围“/subscriptions/XXXX”上执行操作“Microsoft.Consumption/usageDetial/read”或范围无效。如果最近授予访问权限,请刷新您的凭据。

可能是因为我没有使用 APPID 并生成 APPkey 来获取凭据,而是在 Graph API 中获取令牌时使用应用程序的 client_secret ?

4

2 回答 2

2

如果要通过 Azure AD 应用程序访问 Azure 计费 api,我们需要为 AD 应用程序分配 Azure RABC 角色(Billing Reader、Reader、Owner 或 Contributor 角色)。更多详细信息,请参阅文档 在此处输入图像描述

例如(我分配贡献者角色)

第 1 步:登录到您的 azure 门户
第 2 步:在左侧菜单栏中找到订阅并单击。
在此处输入图像描述

第 3 步:单击访问控制 IAM,然后单击添加。在此处输入图像描述

第 4 步:在“添加权限”窗口中,选择角色的贡献者。在选择输入框中,键入您在 Azure AD 中创建的应用名称(在 Azure Active Directory 中创建)并选择它。就我而言,我创建了 Azure 资源管理。在此处输入图像描述

步骤 5:在您授予成功权限后,单击订阅窗口中的刷新,您将看到您的应用程序显示在列表中。请参见下面的示例。 在此处输入图像描述

step6:Powershell脚本

$tenantId="76a1f773...b-86b9-d1ced3e15cda"
$clientId="0159ec7d-f...-a680-c4d40ab7a36c"
$clientSecret="o4eq4jj...I26uz26W~"
$secSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force

$pscredential = New-Object System.Management.Automation.PSCredential ($clientId, $secSecret)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

$dexResourceUrl="https://management.azure.com/"
$context = Get-AzContext
$token = [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, $dexResourceUrl).AccessToken


$SubscriptionId = '3465e081-85b6-4b54-a3e1-15675acb615f'
$billingperiod = '202010-1'

#Create the REST-URL
$usageURL ="https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Billing/billingPeriods/$billingperiod/providers/Microsoft.Consumption/usageDetails?api-version=2017-11-30"

$header = @{
    'Authorization' = "Bearer $($token)"
    "Content-Type" = "application/json"
}
 
$UsageData = Invoke-RestMethod `
    -Method Get `
    -Uri $usageURL `
    -ContentType application/json `
    -Headers $header 

ConvertTo-Json $UsageData
于 2020-08-11T07:06:43.523 回答
0

按照此处的说明进行操作后,在发出实际 API 请求时出现以下错误:

Invoke-RestMethod : {"error":{"code":"500","message":"An error occurred during processing this request. Use this request id
    '17f6fdea-xxxx-xxxx-xxxx-a8c314d770ee' for follow-up."}}
    At C:\$Downloads\billingapitest.ps1:45 char:14
    + $UsageData = Invoke-RestMethod `
    +              ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

任何想法如何解决?

谢谢

达伦

============================================

我向 MS 支持提交了一张票。就在安排演示该问题的会议之前,我再次测试了 Powershell 脚本,发现它现在可以工作了!

于 2020-11-05T16:13:56.210 回答