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

Jorge Ignacio González Méndez 1 Reputation point
2023-09-20T17:49:19.6633333+00:00
$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 AI Metrics Advisor
Azure AI Metrics Advisor
An Azure artificial intelligence analytics service that proactively monitors metrics and diagnoses issues.
82 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,706 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,092 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Chandan Bilvaraj 0 Reputation points
    2023-09-20T17:57:21.34+00:00

    There are a few possible reasons why the Get-AzMetric command might be returning an error when trying to access App Service Plan metrics:

    Incorrect resource URI: Make sure that the resource URI you are using is correct and points to the App Service Plan you want to retrieve metrics for. You can find the resource URI of an App Service Plan in the Azure portal or by running the following PowerShell command:

    Get-AzWebApp -ResourceGroupName <resourceGroupName> -Name <appServiceName> | Select-Object -ExpandProperty ServerFarmId

    Insufficient permissions: Make sure that the account you are using to run the Get-AzMetric command has sufficient permissions to access the App Service Plan. You can check your permissions by running the following PowerShell command:

    Get-AzRoleAssignment -ResourceGroupName <resourceGroupName> -ResourceName <appServicePlanName> | Select-Object -ExpandProperty RoleDefinitionName

    Incorrect metric name: Make sure that the metric name you are using is correct and matches the available metrics for the App Service Plan. You can find a list of the available metrics for an App Service Plan by running the following PowerShell command:

    Get-AzMetricDefinition -ResourceTypeName Microsoft.Web/serverfarms -MetricNamespace Microsoft.Web.Hosting/serverfarms

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.