首页 > 解决方案 > 如何使用 PowerShell 获取 azure 订阅的当前成本

问题描述

我正在尝试使用 PowerShell 获取我的 azure 订阅当前成本。 在此处输入图像描述

期望的输出:

例如:

货币 = 英镑

当前成本 = 370.74

Get-AzConsumptionUsageDetail -BillingPeriodName 202105

但这并没有给我想要的输出。

标签: azurepowershellazure-powershellsubscription

解决方案


以下查询将为您提供上述月份的费用。例如:五月的成本

Get-AzConsumptionUsageDetail -BillingPeriodName 202105

但是,如果您的订阅不是从 5 月 1 日开始的,那么在这种情况下,上述查询会给您错误的费用。例如:您的结算周期是从 5 月 10 日到 6 月 9 日

要了解您的确切计费周期的成本,您需要首先获取当前计费周期,然后根据计费周期开始/结束日期获取成本。

$currentBillingPeriod = Get-AzBillingPeriod -MaxCount 1
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod startDate : " $startDate
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod endDate : " $endDate

$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum
Write-Host "Current Cost of Subscription : " $currentCost.Sum

推荐阅读