Getting the status of a virtual machine is really handy because once you have that information you can then take decisions and perform several actions on your virtual machines like starting it, stopping it, etc.
List all virtual machines in a subscription
To list all the virtual machines in a specific subscription you can execute the following:
Login-AzureRmAccount Select-AzureRmSubscription -SubscriptionName $mySubscriptionName Get-AzureRmVM
List all virtual machines in a specific resource group
To list all the virtual machines of a specific resource group you can execute the following:
Login-AzureRmAccount Select-AzureRmSubscription -SubscriptionName $mySubscriptionName Get-AzureRmVM -ResourceGroupName $myResourceGroupName
Get the status of a specific virtual machine
All the examples above, regardless of the scope to list will give you the base definition of a virtual machine. To get the status of it, you need to provide the
-Status switch. It will take a bit more time, but this time you'll have a completely different response than the previous calls we made. It will contain the information you are looking for in the
Login-AzureRmAccount Select-AzureRmSubscription -SubscriptionName $mySubscriptionName $vm = Get-AzureRmVM -ResourceGroupName $myResourceGroupName -Name 'MyVMName' -Status $vm.Statuses
You will have all the VM information you need to automate your scale up/down and automation scenarios... Enjoy!