I've tried multiple ways to get cost management information from the Azure REST API using Powershell. I'm copy/pasting the data for my $body variable directly from their documentation. I get this error with every example they've posted.
- Below is my original attempt.
- I've tried to pipe $body to convertto-json.
- I've tried saving it as a .json file and then using get-content -raw to get it.
- I've tried defining everything in a $params hash table and then using invoke-restmethod @params
No matter what I get the same error.
invalid query definition, dataset is invalid or not supplied
$method = "post"
$URI = "https://management.azure.com//subscriptions/12345678-1234-1234-1234-1234556789123/resourceGroups/HubRG/providers/Microsoft.CostManagement/query?api-version=2019-11-01"
$headers = @{
"authorization" = "Bearer verylongstringofcharacters"
}
$body = @"
{
"type": "ActualCost",
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalcost" : {
"name": "cost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
"@
Invoke-RestMethod -Method $method -URI $URI -Headers $headers -Body $body