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.
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
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! :)
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.