You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Get-AzMetric获取App Service Plan指标时返回NotFound错误

问题:Azure Runbook执行Get-AzMetric获取App Service Plan指标返回NotFound错误

我在Azure Runbook中执行Get-AzMetric命令获取App Service Plan的指标时,返回了“NotFound”错误。已确认权限配置正确,且查阅了官方文档,但仍未找到解决办法。

代码片段:

$metricParams = @{
    ResourceId = "/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroup)/providers/Microsoft.Web/serverfarms/$($appServicePlanName)/Metrics"
    MetricName = "CPU Percentage"
    Aggregation = "Average"
    StartTime = (Get-Date).AddHours(-1)  # 查询起始时间,例如最近一小时
    EndTime = Get-Date  # 查询结束时间
}

错误堆栈信息:

Exception type: ErrorResponseException, Message: Microsoft.Azure.Management.Monitor.Models.ErrorResponseException: Operation returned an invalid status code 'NotFound'
   at Microsoft.Azure.Management.Monitor.MetricsOperations.<ListWithHttpMessagesAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.Monitor.MetricsOperationsExtensions.<ListAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Commands.Insights.Metrics.GetAzureRmMetricCommand.ProcessRecordInternal()
   at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet(), Code: Null, Status code:Null, Reason phrase: Null

解决办法:

1. 修正ResourceId格式错误

你的ResourceId末尾多了/Metrics后缀,这是核心问题。正确的App Service Plan资源ID格式应为:

/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroup)/providers/Microsoft.Web/serverfarms/$($appServicePlanName)

Get-AzMetric会自动针对目标资源查询指标,无需手动添加/Metrics。

2. 验证指标名称的正确性

虽然“CPU Percentage”是合法指标名,但可先执行以下命令确认该资源支持的所有指标,避免拼写或格式偏差:

Get-AzMetricDefinition -ResourceId "/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroup)/providers/Microsoft.Web/serverfarms/$($appServicePlanName)"

查看返回结果中的Name.LocalizedValue和Name.Value,确保你使用的MetricName与其中的条目完全一致。

3. 检查时间范围有效性

确保StartTime和EndTime符合Azure Monitor的要求:

  • 时间范围不要超过90天(部分指标保留期更短)
  • 避免设置过于精确的时间点,建议保留1-2分钟的缓冲区间

4. 确认Runbook订阅上下文

在执行指标查询前,先明确切换到目标订阅:

Set-AzContext -Subscription $subscriptionId

防止因订阅上下文错误导致资源找不到。


内容的提问来源于stack exchange,提问作者Georgie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 05:13:14