1

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.

  1. Below is my original attempt.
  2. I've tried to pipe $body to convertto-json.
  3. I've tried saving it as a .json file and then using get-content -raw to get it.
  4. 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
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
egrok
  • 55
  • 4
  • On which line do you get the error? The `Invoke-RestMethod` one? Have you tried an extremely simple query? – Nick.Mc Sep 20 '21 at 22:31
  • Seems you should not have "" after @. So it's `$body = @{ ... }` And what about [] in dataset? – Hardoman Sep 23 '21 at 10:10

1 Answers1

0

I had this same issue - drove me crazy for at least an hour. It turned out that even though I was using Invoke-RestMethod, I had to explicitly set 'Content-Type' = 'application/json' in the headers of the request. Without it: 'Dataset is invalid...', with it: success.

$headers = @{
    "authorization" = "Bearer verylongstringofcharacters"
    "Content-Type" = "application/json"
}
Joe Skeen
  • 1,727
  • 16
  • 18