使用PowerShell调用Azure成本管理REST API报数据集无效错误的原因?
问题根因
你提交请求时没有指定Content-Type头为application/json,Azure服务端无法正确识别并解析JSON格式的请求体,因此抛出dataset is invalid or not supplied错误。另外你使用的请求URI中management.azure.com后存在多余的双斜杠,也建议一并修正。
修正后的可用代码
$method = "post" # 修正了URI中多余的双斜杠 $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" } ] } } "@ # 新增ContentType参数指定请求格式为JSON Invoke-RestMethod -Method $method -URI $URI -Headers $headers -Body $body -ContentType 'application/json'
可选排查项(如果修改后仍报错)
- 确认你使用的Bearer令牌有效,且令牌对应的身份对目标订阅/资源组拥有成本管理数据的读取权限
- 确认你使用的API版本与请求体结构兼容,你当前用的2019-11-01版本和提交的body结构是匹配的,不需要额外调整
内容的提问来源于stack exchange,提问作者egrok
相关产品推荐
相关产品推荐

