如何从Powershell查询Azure AppInsights自定义指标

时间:2017-07-24 00:25:02

标签: powershell azure azure-application-insights

我发布了名为" ConnectionCount"的自定义指标。在我的AppInsights组件上。 我正在寻找一种在PowerShell脚本中查询该指标的最后值的方法。

我过去曾使用过这个命令:

$resourceId = "/subscriptions/$subscriptionId/resourceGroups/MyResourceGroup/providers/microsoft.insights/components/MyAppInsight"

Get-AzureRmMetric -ResourceId $resourceId -TimeGrain $timeGrain -StartTime $startTime -MetricNames $metric`

但是当运行这个时,我只是得到一个错误: Get-AzureRmMetric : Operation returned an invalid status code 'NotFound'

我还使用以下代码尝试了REST API:

$resourceGroupId = "subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
$filter = "(name.value eq 'ConnectionCount') and timeGrain eq duration'PT5M' and " +
          "startTime eq 2017-07-20T17:00:47.8791884Z and endTime eq 2017-07-25T17:30:47.8832111Z"
$apiVersion = "2015-05-01"
$uri = "https://management.azure.com/$resourceGroupId/providers/microsoft.insights/components/$appInsightName/metrics?api-version=$apiVersion&`$filter=$filter"
Invoke-RestMethod $uri -Headers $headers

但它只是给了我一个空洞的回应。

有没有人知道Metrics API中是否有某些内容发生了变化,导致无法从PowerShell或REST API检索自定义指标值?

1 个答案:

答案 0 :(得分:0)

如果您收到空的回复(而不是任何类型的失败),那么我建议验证您的查询是否符合您的预期,而不是过滤掉您的所有数据?

你的过滤器对我来说真的很可疑,这部分timeGrain eq duration'PT5M'对我来说似乎是一个奇怪的过滤器。通常你指定范围和谷物作为查询的其他部分,而不是$filter部分

我建议您使用https://dev.applicationinsights.io上的API文档验证所有REST内容,并尝试在以下位置测试您的查询 https://dev.applicationinsights.io/apiexplorer/metrics