miticoluis
miticoluis

Reputation: 531

Get Azure VM status using PowerShell

I'm playing around with Azure's PS commands to try and get the current status of a specific VM. I thought this was going to be straight forward but I was so wrong.

I'm currently using this cmd-let:

Get-AzureRmVM -Name CM01 -ResourceGroupName RG -Status


ResourceGroupName       : RG
Name                    : CM01
Disks[0]                :
  Name                  : CM01
  Statuses[0]           :
    Code                : ProvisioningState/succeeded
    Level               : Info
    DisplayStatus       : Provisioning succeeded
    Time                : 18/08/2016 08:10:20
VMAgent                 :
  VmAgentVersion        : 2.7.1198.778
  ExtensionHandlers[0]  :
    Type                : Microsoft.Azure.RecoveryServices.VMSnapshot
    TypeHandlerVersion  : 1.0.10.0
    Status              :
      Code              : ProvisioningState/succeeded
      Level             : Info
      DisplayStatus     : Ready
      Message           : Plugin enabled (name:     Microsoft.Azure.RecoveryServices.VMSnapshot, version: 1.0.10.0).
  ExtensionHandlers[1]  :
    Type                : Microsoft.Compute.BGInfo
    TypeHandlerVersion  : 1.2.2
    Status              :
      Code              : ProvisioningState/succeeded
      Level             : Info
      DisplayStatus     : Ready
      Message           : Plugin enabled (name: Microsoft.Compute.BGInfo, version: 1.2.2).
  Statuses[0]           :
    Code                : ProvisioningState/succeeded
    Level               : Info
    DisplayStatus       : Ready
    Message             : GuestAgent is running and accepting new configurations.
    Time                : 18/08/2016 14:52:59
Statuses[0]             :
  Code                  : ProvisioningState/succeeded
  Level                 : Info
  DisplayStatus         : Provisioning succeeded
  Time                  : 18/08/2016 12:14:04
Statuses[1]             :
  Code                  : PowerState/running
  Level                 : Info
  DisplayStatus         : VM running

Notice that I use the -Status flag otherwise I wouldn't get any info related to the status of the VM

It returns an object that contains a Statuses array. I don't know how to access the Statuses[1].DisplayStatus position to get the 'VM running' message. Will the VM status always be stored on the Statuses[1]?

This is how I'm trying to get the value (it returns nothing):

Get-AzureRmVM -Name CM -ResourceGroupName RG -Status | Select Statuses[1].DisplayStatus

Statuses[1].DisplayStatus
-------------------------

-Is there an easier way to access a VM status?

Many thanks

Upvotes: 2

Views: 8259

Answers (2)

Stefan
Stefan

Reputation: 308

To make 100%-sure you get the right status (if indexes change), you may want to try to filter the statuses for PowerState/ in code:

$RG = "RG"
$VM = "CM01"
$VMStats = (Get-AzureRmVM -Name $VM -ResourceGroupName $RG -Status).Statuses
($VMStats | Where Code -Like 'PowerState/*')[0].DisplayStatus

Upvotes: 3

David
David

Reputation: 31

Try this command instead:

$RG = "RG01"
$VM = "ADFS"

((Get-AzureRmVM -ResourceGroupName $RG -Name $VM -Status).Statuses[1]).code

Essentially this is PowerShell Inception. You must... go deeper :D

Upvotes: 3

Related Questions