Audit Power State of all VMs | Azure PowerShell

I was recently asked to quickly audit a customer’s environment for all running VMs.  I quickly reached for my PowerShell toolbox and put together the following script.

In the example below I have used the table grid views available.  It would be just as easy to push all this info to a csv file. For swiftness this was my approach, I’d be very interested to hear from the gurus out there to see what your preference is and how you would do this.

Remember there is always a better way, just don’t keep that to yourself!

$FindVMs = Find-AzureRmResource | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}
 
$out = @()
 
Foreach ($vm in $Findvms)
{
$Name = $VM.Name
$ResourceId = $VM.ResourceId
$ResourceGroupName = $VM.ResourceGroupName
$Location = $VM.Location
 
$VMDetail = Get-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status
$props = @{
 
VMName = $VM.Name
ResourceGroup = $VM.ResourceGroupName
Location = $VM.Location
PowerState = $VMDetail.Statuses[1].DisplayStatus
}
$out += New-Object PsObject -Property $props
}
$out | Format-Table -AutoSize -Wrap  ResourceGroup, Location, VMName,Powerstate
$out | Out-GridView -Passthru

Disclaimer:  Please note although I work for Microsoft the information provided here does not represent an official Microsoft position and is provided as is.

(Visited 775 times, 104 visits today)

One thought on “Audit Power State of all VMs | Azure PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *