简体   繁体   中英

Using Get-AzMetric from PowerShell to get “CpuPercentage” metrics from Azure Classic Cloud service returns error

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?

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 .

在此处输入图像描述

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM