Unable to Access [Guest] metrics using Get-AzureRm

2019-03-06 15:38发布

I have guest-level metrics enable for an Azure Virtual Machine and am trying to get the history for the [Guest]\Memory\Committed Bytes property using Get-AzureRMMetric.

$endTime = Get-Date
$startTime = $endTime.AddMinutes(-540)
$timeGrain = '00:05:00'
$metricName = '\Memory\Committed Bytes'


$history=(Get-AzureRmMetric -ResourceId $resourceId `
-TimeGrain $timeGrain -StartTime $startTime `
-EndTime $endTime `
-MetricNames $metricName)

$history.data | Format-table -wrap Average,Timestamp,Maxiumim,Minimum,Total

I get the following error: enter image description here

This code works fine if I change the $metricname to any of the host metrics ("Percentage CPU" for example), but I need to get the memory information.

Note: This is in PowerShell 5.1, I've found that I can use almost the same code ($history.metricvalues rather than $history.data) in PowerShell v3 and I can get to the [Guest] metrics there, but not any of the host metrics.

Powershell v3 example

1条回答
孤傲高冷的网名
2楼-- · 2019-03-06 16:41

For now, Azure PowerShell does not support to use Get-AzureRmMetric to get memory usage metrics.

We can use Get-AzureRmMetricDefinition to get the supported metrics:

Here are the metrics for Azure VM:

PS D:\testdata> (Get-AzureRmMetricDefinition -ResourceId $id).name

Value                     LocalizedValue
-----                     --------------
Percentage CPU            Percentage CPU
Network In                Network In
Network Out               Network Out
Disk Read Bytes           Disk Read Bytes
Disk Write Bytes          Disk Write Bytes
Disk Read Operations/Sec  Disk Read Operations/Sec
Disk Write Operations/Sec Disk Write Operations/Sec
CPU Credits Remaining     CPU Credits Remaining
CPU Credits Consumed      CPU Credits Consumed

About supported metrics of Azure VM, please refer to this official article.

Then we can use the value to get other metrics:

Get-AzureRmMetric -ResourceId $id -TimeGrain 00:01:00 -DetailedOutput -MetricNames "Network in"

Here is my PowerShell output:

enter image description here

As a workaround, we can use OMS to get the Memory usage, more information about configuring performance counters on OMS, please refer to this link.


Update:

You are right, we can run this command on Azure PowerShell version 3.4.0, it works fine.

When we run this command on Version 3.4.0, we will get this warning:

WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet. Here is the PowerShell output: enter image description here

As a workaround, we can via the REST API to export metrics, more information about it, please refer to this link.

查看更多
登录 后发表回答