How to get the status of an #Azure Virtual Machine under the v2 compute model in #PowerShell v0.9.x

Since version 0.8.0, the Azure PowerShell can target multiple APIs, included in 2 PowerShell modules. Even if the name of some cmdlets are the same, they greatly differ in their inputs and responses. First we have the older cmdlets, the PowerShell Azure Service Management Cmdlets and then we have the newer APIs Azure Resource Manage Cmdlets

A newer version of this article for Azure PowerShell v1.0.x is available here:
How to get the status of a Virtual Machine under the v2 compute model in Azure PowerShell v1.0

Do it, the older cmdlets style

In the older API, to get the VM status you would do the following:

Add-AzureAccount -Credential $cred  
Select-AzureSubscription -SubscriptionId $mySubscriptionId  
$vm = Get-AzureVM -ServiceName 'MyCloudService' -Name 'MyVMName'
$vm | Select-Object Status, PowerState

In this example you'll get the state of the VM and cloud service.

Under the newer API (AzureResourceManager), if you call the Get-AzureVM like you would have done in the older API you'll get the deployed definition of the VM but will find no traces of the Virtual machine status.

Switch-AzureMode -Name AzureResourceManager  
Add-AzureAccount -Credential $cred  
Select-AzureSubscription -SubscriptionId $mySubscriptionId  
Get-AzureVM -ResourceGroupName "myRG" -Name "MyVMName"  

It is important to remember that Virtual Machines in the new model (Compute Resource Provider/v2) do not rely on Cloud Services anymore. In the older model, a virtual machine required a Cloud Service to exist, this is not the case anymore.

There is a link to an excellent article about changes in the new IaaS model in the Reference section.

Do it, the newer cmdlets style

To get the status of the virtual machine under the ARM API, you need to specify a new switch in the PowerShell cmdlet named -Status. It will take a little bit more time, but this time you'll have a completely different response than the previous call we made. It will contains the information you are looking for in the Statuses member.

Switch-AzureMode -Name AzureResourceManager  
Add-AzureAccount -Credential $cred  
Select-AzureSubscription -SubscriptionId $mySubscriptionId  
$vm = Get-AzureVM -ResourceGroupName "myRG" -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

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.
Montreal, Canada