Automate Azure VM Status Checks with PowerShell

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:

# 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."