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