Efficiently Monitor System Health Checks in Hyper-V with PowerShell

In this post, we will introduce a PowerShell script that enables IT administrators to perform system health checks on Hyper-V virtual machines. Regular health checks are essential for maintaining optimal performance and quickly identifying any issues within your virtual environment. This script checks the status of all virtual machines, reports on their current state, and flags any that are in critical conditions, allowing for proactive management.
Here is the PowerShell script for performing system health checks on Hyper-V VMs:

# Import Hyper-V module
Import-Module Hyper-V
# Get the list of all VMs and their statuses
$vms = Get-VM
# Create an array to hold health check results
$healthReport = @()
foreach ($vm in $vms) {
    $vmState = $vm.State
    $vmStatus = New-Object PSObject -Property @{
        Name   = $vm.Name
        Status = $vmState
    }
    $healthReport += $vmStatus
    # Check for critical states
    if ($vmState -eq 'Critical' -or $vmState -eq 'Stopped') {
        Write-Host "Warning: VM '$($vm.Name)' is in a critical state: $vmState"
    }
}
# Output the health check report
$healthReport | Format-Table -AutoSize
Write-Host "System health check completed for all Hyper-V VMs."