I am attempting to query Azure Cost Management to query usage for two different dates each for two different resource groups. The value I am trying to query are:
- The current month total usage
- The previous month total usage (relative to the current date), so if today were the 5th, it would query for cost up until the 5th of the previous month.
I keep having null values returned by the API. At first, I thought this may just be because today is 04/01 (the start of a new month)... but no matter how I adjust the timeframe, the result does not seem to change. How do I use Invoke-AzCostManagementQuery to execute a custom query like this?
$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"