Monitoring System Health Checks for Hyper-V Virtual Machines Using PowerShell

Keeping your Hyper-V virtual machines healthy is crucial for the overall performance and reliability of your infrastructure. By automating system health checks, you can easily monitor the status, resource usage, and uptime of your VMs. This PowerShell script enables administrators to quickly assess the health of their Hyper-V virtual machines and take corrective actions when necessary.
This script will:
1. Retrieve the status of all Hyper-V virtual machines.
2. Check the current resource usage, including memory and CPU.
3. Report on the uptime of each VM.
By implementing this script, IT administrators can effectively track the health of virtual machines and ensure optimal performance across their virtualized environments.

# Import the Hyper-V module
Import-Module Hyper-V
# Retrieve all virtual machines
$vms = Get-VM
# Output the health information for each VM
Write-Host "=== Hyper-V Virtual Machine Health Report ==="
foreach ($vm in $vms) {
    $status = $vm.State
    $uptime = (Get-Date) - $vm.Uptime
    $cpuUsage = Get-VMProcessor -VM $vm | Select-Object -ExpandProperty CpuUsage
    $memoryAssigned = "{0:N2} GB" -f ($vm.MemoryAssigned / 1GB)
    Write-Host "VM Name: $($vm.Name)"
    Write-Host "  Status: $status"
    Write-Host "  Uptime: $([math]::round($uptime.TotalHours, 2)) hours"
    Write-Host "  CPU Usage: $cpuUsage %"
    Write-Host "  Assigned Memory: $memoryAssigned"
    Write-Host "-------------------------------------"
}
Write-Host "Health check completed."