Skip to content

Instantly share code, notes, and snippets.

@mmckechney
Created May 1, 2020 20:13
Show Gist options
  • Save mmckechney/5c47d4e94da78cfc031f8ef1ffbb87f3 to your computer and use it in GitHub Desktop.
Save mmckechney/5c47d4e94da78cfc031f8ef1ffbb87f3 to your computer and use it in GitHub Desktop.
Display Power State for all VMs

Intro:

This PowerShell script will iterate through all of the Azure subscriptions and VMs you have access to get the following information:

  • Subscription ID
  • Resource Group Name
  • VM Name
  • Location
  • Size
  • OS Type (Windows or Linux)
  • Power State

To run the script

  1. Either run in an Azure Cloud Shell window or run Connect-AzAccount to login to Azure PowerShell
  2. Run .\Get-AllVMPowerState.ps1
$vmInfo = @()
class vmdata {
[string]$subscription
[string]$resourceGroupName
[string]$Name
[string]$Location
[string]$Size
[string]$OS
[string]$PowerState
}
$subs = Get-AzSubscription
foreach($sub in $subs)
{
Write-Output "Subscription: $($sub.Name)"
Remove-AzContext -InputObject (Get-AzContext) -Force | Out-Null;
$ctx = Set-AzContext -SubscriptionObject $sub
$tmp = Get-AzVM
foreach($t in $tmp){
$vm = [vmdata]@{
subscription= $sub
resourceGroupName = $t.ResourceGroupName
Name = $t.Name
Location = $t.Location
Size = $t.HardwareProfile.VmSize
OS = ($null -eq $t.OSProfile.WindowsConfiguration) ? "Linux" : "Windows"
PowerState = (Get-AzVm -ResourceGroupName $t.ResourceGroupName -Name $t.Name -Status | Select-Object -expandproperty Statuses | Where-Object -Property Code -match "PowerState*").DisplayStatus
}
$vmInfo += $vm
}
}
$vmInfo | Format-Table -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment