In this article, I will show you how to get Azure VMs status using Azure powershell script.

Azure VMs status

To check status of your servers, you need few things:

  • Azure Credentials
  • Environment
  • TenantID
  • Subscription
  • Resource Group Name

Below you can find an example for converting your password to secure string and adding it with account ID to azure credentials variable:

1- $an example = ConvertTo-SecureString "67t1uS5HfXCc9L4d3P02" -AsPlainText -Force
2- $AzureCred = New-Object System.Management.Automation.PSCredential ("[email protected]", $SecPasswd)

Now, when credentials are set you can use Add-AzureRmAccount which adds an authenticated account to use for Azure Resource Manager cmdlet requests.

Add-AzureRmAccount -Environment AzureBros -TenantId e0527c39-0369-376d-b139-342dcd316geb -Subscription 'PowerShellBros 1' -Credential $AzureCred

Below you can find a simple script on how to get status of your VMs using Get-AzureRmVM command. I wanted to display the following properties ResourceGroupName, Name, Location, PowerState:

1- $SecPasswd = ConvertTo-SecureString "67t1uS5HfXCc9L4d3P02" -AsPlainText -Force
 $AzureCred = New-Object System.Management.Automation.PSCredential ("[email protected]", $SecPasswd)

2- Add-AzureRmAccount -Environment AzureBros -TenantId e0527c39-0369-376d-b139-342dcd316geb -Subscription 'PowerShellBros 1' -Credential $azurecred

3- #Get VM status and add results to variable
 $AllVMs = Get-AzureRmVM -ResourceGroupName ps_bros -Status | Select-Object ResourceGroupName,Name,Location, @{ label = “VMStatus”; Expression = { $_.PowerState } }

4- #Or get VM status where server names match VM and add results to variable
 $AllVMs = Get-AzureRmVM -ResourceGroupName ps_bros -Status | Where-Object {$ -match "VM"} | Select-Object ResourceGroupName,Name,Location, @{ label = “VMStatus”; Expression = { $_.PowerState } }

5- #Display results in console in Wrap Mode
 $AllVMs | Format-Table -Auto -Wrap

6- #Display results in new window call Grid view
 $AllVMs | Out-GridView -Title "Azure VMs"

7- #Display running VMs in console
 $AllVMs | Where-Object {$_.Status -eq "VM running"}

8-#Export results to CSV file and save on local machine
 $AllVMs | Export-Csv "c:\users\$env:username\documents\Azure_VMs_Status.csv" -Force -NoTypeInformation

I hope this small trick will help to get VM status quickly. If you need more advance and declarative script, you should check another post to get advance script.