Get Azure VM Metric Information with PowerShell – [ENG]

By | 07 January 2019

Hi all,

I decided to write some articles in English on my blog. Because I want to access not only Turkish techies  but also others from different countries :) That’s why every now and then you will see English posts on my blog.

With this article I will show you how to get Azure Vms cpu usage information using powershell. It is pretty easy and very useful if you want to create html dashboard which gets data from powershell or a log file.

To get the necessary CPU usage information from a Azure VM you need to use Get-AzureRMMetric cmdlet. You can find detailed information about this cmdlet using the link below.

https://docs.microsoft.com/en-us/powershell/module/azurerm.insights/get-azurermmetric?view=azurermps-6.13.0

If you want to know supported metrics for Azure VM, you can use Get-AzureRMMetricDefiniton command.

By running this command, you can decide which metric you have to use. And it will also be useful to list the metrics that you will use as parameter values.

If you want to get VM metric information, First of all you need to get Azure VM. For this basic process you can use that command below. (Please fill $RGName and $VMName parameter values with yours.)

After Vm is assigned to the variable, you can use that variable as a parameter for Get-AzureRMMetric command.

For this process you can run the command below.

I want to explain the properties of this command more,

  • TimeGrain: Specifies the time grain of the metric as a TimeSpan object in the format hh:mm:ss
  • Metric Names: Specifies an array of names of metrics.

Because this command returns an array about CPU metrics, I select only Average value. (I used –expandproperty command to get only value)

I also used an if statement in this script. Because sometimes Vm can be  in stopped state. Because of this you can not get metric information from this VM. That‘s why this statement will notify you about Vm status.

The below calculation is necessary to round that values to spesific decimal place.

I hope it helps.

See you next article.

Fırat

 

Leave a Reply