Check if a VM is stopped or deallocated from the Azure fabric with PowerShell.

Check if a VM is stopped or deallocated from the Azure fabric with PowerShell.

Q. How can I check from PowerShell if a VM is shutdown or actually deallocated in Azure?

A. The Get-AzureRmVM cmdlet has a -Status parameter which shows a number of aspects related to the VM. An array of statuses are found for each VM and you need to look at the DisplayStatus attribute of the multiple statuses which do not have a code of ProvisioningState/succeeded. For example:

$VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status
foreach ($VMStatus in $VMDetail.Statuses)
{ 
    if($VMStatus.Code.CompareTo("ProvisioningState/succeeded") -ne 0) #don't want to the provisioning status
    {
        $VMStatusDetail = $VMStatus.DisplayStatus
    }
}

If the VM is stopped but still provisioned on the Azure fabric (you are still paying) it will have a value of "VM stopped". If the VM is deprovisioned from the fabric it will have a value of "VM deallocated".

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish