In the Azure Portal under Cost Management, Cost Analysis, the Actual Cost and Forecast amounts are displayed. I want to get the forecast amount using the rest API and power shell. I have looked at the Forecast documentation and similar posts on stackoverflow but getting nowhere. I have a ticket open with Microsoft since late May 2022, but they have not been helpful.
Here is what I am doing:
I have a file called jsonfile.txt :
{
type: "Usage",
dataset: {
"granularity": "monthly",
"aggregation":{
"totalCost":{
"name":"Cost",
"function":"Sum"
}
}
},
timeframe: "MonthToDate"
}
I am not sure that this is the correct format for the file, am I missing anything?
I put that into the $body variable :
$body = Get-Content .\jsonfile2.txt -Raw
Log in and get the subscription ID ($subID) and token on the PS command line:
$azContext = Get-AzContext -ErrorAction SilentlyContinue
$subID = $azContext.Subscription.id
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
'Content-Type'='application/json'
'Authorization'='Bearer ' + $token.AccessToken
}
Set the URL :
$forecasturi = "https://management.azure.com/subscriptions/$subid/providers/Microsoft.CostManagement/forecast?api-version=2021-10-01"
then call the URL with header, method and body :
$theFC = invoke-restmethod -uri $forecasturi -Method post -headers $authHeader -body $body
and I just get this error:
invoke-restmethod : {"error":{"code":"BadRequest","message":"Invalid query definition: Invalid dataset grouping: 'BillingPeriod'; valid values: 'ResourceGroup','ResourceGroupName','ResourceType','ResourceI
d','ResourceLocation','SubscriptionId','SubscriptionName','MeterCategory','MeterSubcategory','Meter','ServiceFamily','UnitOfMeasure','PartNumber','BillingAccountName','BillingProfileId','BillingProfileName
','InvoiceSection','InvoiceSectionId','InvoiceSectionName','Product','ResourceGuid','ChargeType','ServiceName','ProductOrderId','ProductOrderName','PublisherType','ReservationId','ReservationName','Frequen
cy','InvoiceId','PricingModel','CostAllocationRuleName','MarkupRuleName','BillingMonth','Provider','BenefitId','BenefitName','CustomerTenantId','CustomerTenantDomainName','ResellerMPNId','PartnerEarnedCred
itApplied','CustomerName','PartnerName',''.\r\n\r\n (Request ID: 66c872c6-57af-4741-92bb-a5130ceae505)"}}
At line:1 char:10
- $theFC = invoke-restmethod -uri $forecasturi -Method post -headers $ ...
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I tried this, https://stackoverflow.com/questions/69821974/forecast-cost-azure-api, but no luck.
Any help would be appreciated.