gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:20:59 26 4
gpt4 key购买 nike

我正在尝试查询 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com