/ PowerShell

How to get the status of a Virtual Machine under the v2 compute model in Azure PowerShell v1.0.x

Getting the status of a virtual machine is really handy because once you have that information you can then take decisions and perform several actions on your virtual machines like starting it, stopping it, etc.

List all virtual machines in a subscription

To list all the virtual machines in a specific subscription you can execute the following:

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $mySubscriptionName
Get-AzureRmVM

List all virtual machines in a specific resource group

To list all the virtual machines of a specific resource group you can execute the following:

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $mySubscriptionName
Get-AzureRmVM -ResourceGroupName $myResourceGroupName

Get the status of a specific virtual machine

All the examples above, regardless of the scope to list will give you the base definition of a virtual machine. To get the status of it, you need to provide the -Status switch. It will take a bit more time, but this time you'll have a completely different response than the previous calls we made. It will contain the information you are looking for in the Statuses member.

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $mySubscriptionName
$vm = Get-AzureRmVM -ResourceGroupName $myResourceGroupName -Name 'MyVMName' -Status
$vm.Statuses

You will have all the VM information you need to automate your scale up/down and automation scenarios... Enjoy!

References

Stephane Lapointe

Stephane Lapointe

I am the proud father of two little gems. A beer & wine enthusiasm. For everything else, I work and play with Azure at day, and I am an Azure MVP & Advisor at night.

Read More