Identify VM’s running status in Azure Resource Manager

Unlike classic model, there is no direct cmdlet to know the status for virtual machine provisioned using resource manager model. We can use the following commands to know the status of a particular virtual machine:

$resourceGroupName = "sharepointvms"
$vmName = "indspstd01"
$vmStatuses = $(Get-AzureRmVM -ResourceGroupName $resourceGroupName -VMName $vmName -Status).Statuses
foreach($vmStatus in $vmStatuses){
 if($vmStatus.Code -ne "ProvisioningState/succeeded")
 {
 Write-Output "$vmName status is `"$($vmStatus.displaystatus)`""
 }
}

Replace the value of $resourceGroupName and $vmName appropriately, in your environment. You should get an output like below:

identify virtual machine status in azure resource manager
identify virtual machine status in azure resource manager

You can use below script to get status for all VMs in your subscription in one go:

$resourceGroups = Get-AzureRmResourceGroup

$vmstatus = @()

foreach($resourceGroup in $resourceGroups){
 $vms = Get-AzureRmVM -ResourceGroupName $($resourceGroup.ResourceGroupName)
 foreach($vm in $vms){
 $statuses = $(Get-AzureRmVM -ResourceGroupName $($resourceGroup.ResourceGroupName) `
 -Name $($vm.Name) -Status).Statuses
 foreach($status in $statuses){
 if($status.Code -notlike "*provisioning*"){
 $vmRunningStatus = $status.Code.Split('/')[1]

$vmstatusObject = New-Object -TypeName PSObject
 $vmstatusobject | Add-Member -MemberType NoteProperty -Value $resourceGroup.ResourceGroupName -Name ResourceGroupName
 $vmstatusobject | Add-Member -MemberType NoteProperty -Value $vm.Name -Name VMName
 $vmstatusobject | Add-Member -MemberType NoteProperty -Value $vmRunningStatus -Name VMStatus
 $vmstatus += $vmstatusobject
 }
 }
 }

}

This should generate a nice output like below:

Status for all ARM VMs associated with a subscription
Status for all ARM VMs associated with a subscription

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s