To get the CPU usage for an Azure app service using PowerShell:
There is an Azure PowerShell command called Get-AZMetric
to get all the metrics data from a particular resource (Eg: App Service). However, CPU %
is not a supported argument for Get-AZMetric
, if you copy the resourceID
of the Web App properities as it is a cloud service ID.
We can achieve it for overall AppServicePlan
as CPU %
supports here.
If the app services hosted in premium, standard and basic plans, CPU%
will comes because of scaling out feature whereas CPU_Time
is helpful when hosted in free or the shared app service plans.
get-azmetric -resourceID "/subscriptions/<subscriptionID>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/sites/<AppService>" -MetricName "CpuTime" -DetailedOutput

After investigating, I've found a way to achieve it with the help of AppServicePlan by taking resourceID as shown:

az monitor metrics list --resource "/subscriptions/<subscriptionID>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/serverfarms/<AppservicePlan>" --metrics "CpuPercentage","MemoryPercentage"
Metrics for CPU Percentage:

Metrics for Memory Percentage:

Note:
- Check the supported metrics through portal by clicking on
"Diagnose and solve problems"
if needed.

- Issues with AppService environments