0
votes

I have a fairly simple requirement that is giving me some grief. I am trying to use the Get-AzMetric cmdlet for PowerShell to extract average percent CPU usage metrics for an Azure Classic Cloud service. (This would be the same information as found in the portal under Monitoring -> Metrics.)

For my simple script I have isolated to the following:

Import-Module Az
Connect-AzAccount

Get-AzMetric `
    -ResourceId "/subscriptions/8xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxf/...xxx" `
    -TimeGrain 00:15:00 `
    -MetricName "CpuPercentage"

The ResourceId is copied and pasted directly from the Azure portal for the Classic Cloud service that I am trying to get the metrics for. From all appearances I am following the documentation correctly from Get-AzMetric (As.Monitor) | Microsoft Docs, however I keep getting a BadRequest response that is not particularly insightful:

Get-AzMetric : Exception type: ErrorResponseException, Message:
    Microsoft.Azure.Management.Monitor.Models.ErrorResponseException:
    Operation returned an invalid status code 'BadRequest'

Any ideas on what might be wrong with my request?

1

1 Answers

1
votes

Well, in short, two errors in this case.

1.If you copy the Resource ID directly from the Properties in the portal, it is the resource id for the cloud service, its resource type is Microsoft.ClassicCompute/domainNames, which is not supported by the Get-AzMetric command.

Actually, the information under Metrics in the portal is for the web role, its resource type is Microsoft.ClassicCompute/domainNames/slots/roles.

enter image description here

2.The MetricName in your command is wrong, it should be Percentage CPU, not CpuPercentage.


So in conclusion, actually your command is used to get the metric of the web role in the cloud service, please change the parameters like below, it will work.

$metric = Get-AzMetric -ResourceId "/subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.ClassicCompute/domainNames/<cloudservice-name>/slots/production/roles/WebRole1" -TimeGrain 00:15:00 -MetricName "Percentage CPU"
$metric.Data

enter image description here