Monitoring System Health Checks in Hyper-V with PowerShell

Welcome to our blog dedicated to sharing useful PowerShell scripts designed to optimize your IT management tasks! In this post, well focus on a PowerShell script that automates system health checks for virtual machines running in Hyper-V. Regular health checks are vital for ensuring that your virtual environments are functioning correctly and efficiently.
This script will help you monitor CPU usage, memory allocation, and disk space for each virtual machine, allowing you to identify potential issues before they impact your operations. For more powerful IT solutions, be sure to check out our software, ServerEngine, at [https://serverengine.co](https://serverengine.co).
### Step 1: Define the Hyper-V Host
The first step is to identify the Hyper-V host you want to monitor. This will allow you to retrieve information about the virtual machines running on that host.
“`powershell

# Define the Hyper-V host
$hyperVHost = "localhost"  # You may replace with the hostname or IP address of your Hyper-V host

“`
### Step 2: Get Virtual Machines
Next, use the `Get-VM` cmdlet to retrieve a list of all virtual machines running on your specified Hyper-V host.
“`powershell

# Get all virtual machines from the Hyper-V host
$vms = Get-VM -ComputerName $hyperVHost
Write-Host "Found $($vms.Count) virtual machines on host $hyperVHost."

“`
### Step 3: Check CPU Usage
For each virtual machine, we will check the CPU usage. This step uses the `Get-VMProcessor` cmdlet to monitor the CPU performance of each VM.
“`powershell

# Check CPU usage for each virtual machine
foreach ($vm in $vms) {
    $cpuUsage = Get-VMProcessor -VM $vm
    Write-Host "$($vm.Name) CPU Usage: $($cpuUsage.LoadPercentage)%"
}

“`
### Step 4: Check Memory Usage
Next, we will assess the memory usage for each virtual machine. This is done using the `Get-VMMemory` cmdlet to monitor the allocated memory.
“`powershell

# Check Memory usage for each virtual machine
foreach ($vm in $vms) {
    $memory = Get-VMMemory -VM $vm
    Write-Host "$($vm.Name) Memory Assigned: $($memory.MemoryStartup) MB - Memory Demand: $($memory.MemoryDemand) MB"
}

“`
### Step 5: Check Disk Space
Lastly, we will check the virtual disks associated with the virtual machines to ensure they have sufficient space. This utilizes the `Get-VMHardDiskDrive` cmdlet.
“`powershell

# Check Disk space for each virtual machine
foreach ($vm in $vms) {
    $hardDisks = Get-VMHardDiskDrive -VM $vm
    foreach ($disk in $hardDisks) {
        $diskSize = Get-VHD -Path $disk.Path
        Write-Host "$($vm.Name) Disk: $($disk.Path) - Size: $([math]::round($diskSize.FileSize/1GB, 2)) GB - Used: $([math]::round($diskSize.Size/1GB, 2)) GB"
    }
}

“`
### Step 6: Summary of Health Check
Finally, you can summarize the health checks performed to provide an overview of the system states.
“`powershell

# Summary output
Write-Host "Health checks completed for virtual machines on host $hyperVHost."
Write-Host "Please ensure that you monitor any instances where usage exceeds expected thresholds."

“`
With this script, youll be able to automate system health checks in Hyper-V effectively, ensuring your virtual environment runs smoothly. For more comprehensive automation tools and enhancements, visit [ServerEngine](https://serverengine.co). Happy scripting!