4
votes

I used Azure Resource Manager to create a resource group with a couple of VMs and other resources.

How can I read a VM status for a VM that was provisioned, using Azure Resource Manager (ARM), as a resource in a resource group ?

With classic VMs, old scripts use:

$vm.InstanceStatus

However, when I switch to the Azure Resource Manager mode

Switch-AzureMode AzureResourceManager

The VM object doesn't recognize the .InstanceStatus as a valid variable.

4

4 Answers

4
votes

The PowerShell one-liner that you are looking for might look something like this:

Get-AzureRmVM -ResourceGroupName "MyRGName" -Name "MyVmName" -Status | `
    select -ExpandProperty Statuses | `
    ?{ $_.Code -match "PowerState" } | `
    select -ExpandProperty DisplayStatus

This will show VM deallocated if the VM is stopped or VM running if it's running.

To get hold of the Get-AzureRmVm cmdlet, run the following:

Install-Package AzureRM
Install-AzureRM
0
votes

Try the following:

Get-AzureVM -ResourceGroupName <RGName> -Name <VMName> -Status

In the result, you should see Statuses, and within this you should see one that looks like "PowerState/running", etc.

Hope this helps! :)

0
votes

Hi I have provided a script to be able to do this thanks to the examples above.

https://miteshc.wordpress.com/2016/02/24/automation-runbook-shutdown-azurearmvm-with-tags/

extract

$ResourceGroup = "RG_Name" $VMs = Find-AzureRmResource -ResourceGroupNameContains $ResourceGroup | Where-Object {$_.Tags.Name-eq "Autoshutdown" -and $_.Tags.Value -eq "Yes"} Foreach ($VM in $VMs) { $VMStatus = Get-AzureRmVM -ResourceGroupName $ResourceGroup -Name $vm.Name -Status | select -ExpandProperty Statuses | ?{ $_.Code -match "PowerState" } | select -ExpandProperty displaystatus

   if($VMStatus -eq "VM Running")
       {
          Write-Output "status of" $vm.Name "is" """$VMStatus"""
          Stop-AzureRmVM -ResourceGroupName $ResourceGroup -Name $vm.Name -Force
       }
       else
       {
          Write-Output "status of" $vm.Name "is" """$VMStatus"""
       }

}

Thanks ! Mitesh

-1
votes

You can read the provisioning status of a Azure VM provisioned under ARM using the PowerShell code below:

$vm = Get-AzureRmVM -Name <VMName> -ResourceGroupName <ResourceGroupName>
$vm.ProvisioningState

This is tested with the latest Azure PowerShell revision (1.0.1) which is released on November 2015.