Get-AzMetric command is returning error when trying to access App Service Plan metrics

67 views Asked by At

This is my code: i check permission correct and check documentation but i dont found de answer.

$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

Error in my job azure runbook whnen i run it..

1

There are 1 answers

0
SiddheshDesai On

According to this Document, The Get-AzMetric command uses microsoft.web/sites resource type and resource id to get the metrics which is the metrics of Azure Web app and Azure web app does not contain CPU Percentage metrics. CPU Percentage metrics is included in App Service plan. For Azure App service plan metrics you need to refer this Document2 which uses Get-AzAppServicePlanMetrics command to get CPU Percentage metrics of App Service Plan:-

First get the resource id with resource type microsoft.web/sites to use it in your command like below:-

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

Output:-

enter image description here

Use this ResourceID in your Get-AzMetrics command like below:-

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

Output:-

enter image description here

In order to get App Service Plan CPU percentage, Refer the script from this 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

Reference - Using Get-AzMetric from PowerShell to get "CpuPercentage" metrics from Azure Classic Cloud service returns error By Joy Wang