尝试访问应用服务计划指标时,Get-AzMetric 命令返回错误

问题描述 投票:0回答:1

这是我的代码: 我检查权限是否正确并检查文档,但我没有找到答案。

$metricParams = @{
    ResourceId = "/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroup)/providers/Microsoft.Web/serverfarms/$($appServicePlanName)/Metrics"
    MetricName = "CPU Percentage"
    Aggregation = "Average"
    StartTime = (Get-Date).AddHours(-1)  # Inicio de la consulta, por ejemplo, para la última hora
    EndTime = Get-Date  # Fin de la consulta
}
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

我的工作 azure runbook 运行时出错..

powershell metrics azure-app-service-plans azure-runbook
1个回答
0
投票

根据这个Document

Get-AzMetric
命令使用
microsoft.web/sites
资源类型和资源id来获取指标这是Azure Web应用程序的指标并且Azure Web应用程序不包含CPU百分比指标。 CPU 百分比指标包含在应用服务计划中。 对于 Azure 应用程序服务计划指标,您需要参考此 Document2,它使用
Get-AzAppServicePlanMetrics
命令来获取应用程序服务计划的 CPU 百分比指标:-

首先获取资源类型为 microsoft.web/sites 的资源 ID,以便在命令中使用它,如下所示:-

Get-AzResource -ResourceGroupName siliconrg -ResourceType Microsoft.web/sites

输出:-

enter image description here

在 Get-AzMetrics 命令中使用此 ResourceID,如下所示:-

Get-AzMetric -ResourceId "/subscriptions/sub-id/resourceGroups/siliconrg/providers/Microsoft.Web/sites/siliconwebapp096" -TimeGrain 00:01:00 

输出:-

enter image description here

为了获取应用服务计划 CPU 百分比,请参阅此脚本 Document2:-


$currentTime = Get-Date -UFormat "%Y-%m-%dT%H:%M:%SZ"


$endTime = $currentTime

$startTime = (Get-Date).AddHours(-1)


$startTimeFormatted = $startTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")


Write-Host "UTC Start Time: $startTimeFormatted"
Write-Host "UTC End Time: $endTime"

Get-AzAppServicePlanMetrics -ResourceGroupName "Resource-group-name" -Name "Appservice-plan-name" -StartTime 2023-09-23T07:21:06Z -EndTime 2023-09-23T13:51:06Z -Granularity PT1M -Metrics ["CPU Percentage"] -Debug
© www.soinside.com 2019 - 2024. All rights reserved.