In this post, we will introduce a PowerShell script that efficiently monitors the health of Hyper-V virtual machines in your environment. Regular health checks are vital for ensuring the performance and availability of your virtual infrastructure. This script will analyze the status of each virtual machine and provide a concise report, helping administrators proactively manage their resources.
Here is the PowerShell script for monitoring Hyper-V virtual machine health:
# Import the Hyper-V module
Import-Module Hyper-V
# Get all Hyper-V virtual machines
$vms = Get-VM
# Create an array to hold health check results
$healthReport = @()
# Check each VM status and gather information
foreach ($vm in $vms) {
$status = if ($vm.State -eq 'Running') { "Healthy" } else { "Critical" }
$healthReport += [PSCustomObject]@{
Name = $vm.Name
Status = $status
State = $vm.State
MemoryUsageGB = [math]::Round($vm.Processor.UsagePercentage, 2)
}
}
# Output health status in a formatted table
$healthReport | Format-Table -AutoSize
Write-Host "System health check completed for Hyper-V virtual machines."