azure - Powershell Invoke-AzCostManagementQuery for 'custom' 时间帧

标签 azure powershell cost-management

我正在尝试查询 Azure 成本管理,以查询两个不同资源组的两个不同日期的使用情况。我试图查询的值是:

  1. 当月总使用量
  2. 上个月的总使用量(相对于当前日期),因此如果今天是 5 号,它将查询截至上个月 5 号的费用。

API 一直返回空值。起初,我以为这可能只是因为今天是 04/01(新的一个月的开始)......但无论我如何调整时间范围,结果似乎都没有改变。如何使用 Invoke-AzCostManagementQuery 执行这样的自定义查询?

    $prev_month_same_day = (Get-Date).AddMonths(-1).AddDays(-1)
    $first_of_prev_month = [DateTime]::ParseExact(($prev_month_same_day | Get-Date -Format "MM/01/yyyy"), 'MM/dd/yyyy', $null)

    $scope_managed_rg = "/subscriptions/" + $subscription_id + "/resourceGroups/" + $db_managed_rg
    $scope_unmanaged_rg = "/subscriptions/" + $subscription_id + "/resourceGroups/" + $db_unmanaged_rg

    $rg1_currMonthRaw = Invoke-AzCostManagementQuery -Type "Usage" -scope $scope_managed_rg -timeframe "MonthToDate"
    $rg2_currMonthRaw = Invoke-AzCostManagementQuery -Type "Usage" -scope $scope_unmanaged_rg -timeframe "MonthToDate"
    $rg1_prevMonthRaw = Invoke-AzCostManagementQuery -Type "Usage" -scope $scope_managed_rg -TimePeriodFrom $first_of_prev_month -TimePeriodTo $prev_month_same_day -Timeframe "Custom"
    $rg2_prevMonthRaw = Invoke-AzCostManagementQuery -Type "Usage" -scope $scope_unmanaged_rg -TimePeriodFrom $first_of_prev_month -TimePeriodTo $prev_month_same_day -Timeframe "Custom"

最佳答案

尝试以下操作:

$today = [DateTime]::Now
$month = $today.Month
$year = $today.Year
$day = $today.Day
if($month -eq 1)
{
   $year -= 1
   $month = 12
} else
{
   $month -= 1
}
$lastMonth = [DateTime]::New($year, $month, $day)
$lastMonth

关于azure - Powershell Invoke-AzCostManagementQuery for 'custom' 时间帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75904868/

相关文章:

SQL Azure : Executing SQL directly; no cursor

c# - 服务总线 token 的签名无效

powershell - PowerShell ISE和PowerShell.Exiting事件

powershell - 使用Microsoft Graph API将文件上传到SharePoint Online

azure - 在azure消费使用详细信息api中,指标类型actualcost、usagecost和amortizedcost之间有什么区别?

azure - 我似乎从 azure ad 获取了错误的不记名 token ,APM API 未经授权

c# - 如何在 Microsoft.ApplicationInsights.AspNetCore“版本 ="2.20.0"及更高版本中禁用 Application Insights 日志记录?

arrays - 通过 PowerShell 中的 Foreach 循环动态标点和语法更正字符串

google-cloud-functions - 有什么方法可以将我的 Google Cloud Functions 支出分解为我的每个功能?

python : Cost of forming list with comprehension vs standalone function