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..
According to this Document, The
Get-AzMetriccommand usesmicrosoft.web/sitesresource 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 usesGet-AzAppServicePlanMetricscommand 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:-
Output:-
Use this ResourceID in your Get-AzMetrics command like below:-
Output:-
In order to get App Service Plan CPU percentage, Refer the script from this Document2:-
Reference - Using Get-AzMetric from PowerShell to get "CpuPercentage" metrics from Azure Classic Cloud service returns error By Joy Wang