← script library

Automate Azure VM Status Checks with PowerShell

Microsoft 365 & Entra IDDecember 24, 2024

In this post, we will introduce a PowerShell script that automates the process of checking the status of Azure virtual machines. Monitoring the health and status of your virtual machines is vital for ensuring their performance and availability. This script retrieves the current status of all Azure VMs in your subscription, allowing administrators to quickly identify any issues and take necessary actions. Here is the PowerShell script for checking the status of Azure VMs:

powershell
# Connect to Azure Account
Connect-AzAccount
# Get all Azure VMs
$vms = Get-AzVM
# Create an array to hold status information
$vmStatusReport = @()
# Iterate through each VM and retrieve its status
foreach ($vm in $vms) {
    $vmDetails = Get-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
    $vmStatusReport += [PSCustomObject]@{
        VMName        = $vm.Name
        Status        = $vmDetails.PowerState
        Location      = $vm.Location
    }
}
# Output the status report
$vmStatusReport | Format-Table -AutoSize
Write-Host "Azure VM status check completed successfully."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.