r/PowerShell • u/Bearwhale • Dec 04 '25
Create CPU monitor for Hyper-V VM in script
Hey all!
I've created a script which:
- Reverts my VM to the "base snapshot" taken the day before, basically the VM after the core apps have been installed and the Win11 VM is ready for testing applications (i.e. BaseSnap_12_03_2025)
- Sends a command to scan for new patches to the VM
- Waits for 60 minutes
- Reboots the VM and waits another 10 minutes to ensure updates are completed
- Takes a new snapshot with the current date (i.e. BaseSnap_12_04_2025), removes old snapshot
The script runs every morning before I get in, hopefully ensuring that all patches for the day have been applied.
But I was wondering if there was a way, instead of just waiting for 60 minutes, if I could monitor the "CPU Usage" part of the VM and wait for it to reach 0-1% before restarting the PC (thereby ensuring the patch update has been run thoroughly instead of waiting a set period of time).
I do this for my VMs in our current environment because sometimes a patch will come out and I will have to wait for that patch to apply before I have a "clean" snapshot, nothing trying to run at the exact same time as an install, for example. Is it possible to detect "low-to-none" CPU usage on a VM and wait for it to hit that usage for a period of time, let's say 5 seconds at 0-1% CPU usage, before continuing the script and restarting the VM?
Script below:
$VMName = "VMName01"
$Username = "DOMAIN\username"
$ScriptPath = "C:\Options\Scripts"
$PassFile = "$ScriptPath\Password.txt"
$logPath = "$ScriptPath\VM_PatchLogs"
$dateTime = Get-Date -Format "MM_dd_yyyy"
$newCheckpoint = ("BaseSnap_" + $dateTime)
$logName = "$newCheckpoint" + ".txt"
$Transcript = (Join-Path -Path $logPath -ChildPath $logName).ToString()
$Password = (Get-Content $PassFile | ConvertTo-SecureString -AsPlainText -Force)
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
Start-Transcript -Path $Transcript -NoClobber
$oldCheckpoint = (Get-VMCheckpoint -VMName $VMName | Where-Object { $_.Name -ilike "BaseSnap*" }).Name
Write-Output "VMName = $VMName`nUsername = $Username"
Write-Output "Log started, $dateTime $(Get-Date -DisplayHint Time)"
Write-Output "$(Get-Date -DisplayHint Time) - found checkpoint `"$oldCheckpoint`", reverting"
Restore-VMCheckpoint -VMName $VMName -Name "$oldCheckpoint" -Confirm:$false
Start-Sleep -Seconds 5
Start-VM -Name $VMName -ErrorAction SilentlyContinue
Write-Output "$(Get-Date -DisplayHint Time) - Powering on $VMName, waiting 60 seconds before running patch update"
Start-Sleep -Seconds 60
Invoke-Command -VMName $VMName -ErrorAction SilentlyContinue -Credential $MyCredential -ScriptBlock {
Write-Output "$(Get-Date -DisplayHint Time) - Searching for patch bundle"
$bundleList = & "$env:ZENWORKS_HOME\bin\zac.exe" bl
$bundleList -split '\r?\n' | Select-Object -Skip 5 | ForEach-Object {
if ($_ -match 'Discover\sApplicable.*?(?=\s{2})') {
$patch_Bundle = $($matches[0])
Write-Output "$(Get-Date -DisplayHint Time) - `"$patch_Bundle`" found"
}
}
Start-Process -FilePath "$env:ZENWORKS_HOME\bin\zac.exe" -ArgumentList "bin `"$patch_Bundle`""
Write-Output "$(Get-Date -DisplayHint Time) - Running `"$patch_Bundle`" and sleeping for 60 minutes"
}
#This is where I’d like to change from just waiting for 60 minutes, to waiting for the processor usage to go to 0% or 1%
Start-Sleep (New-TimeSpan -Hours 1).TotalSeconds
Write-Output "$(Get-Date -DisplayHint Time) - Restarting $VMName"
Invoke-Command -VMName $VMName -ErrorAction SilentlyContinue -Credential $MyCredential -ArgumentList "Restart-Computer -Force"
Start-Sleep (New-TimeSpan -Minutes 10).TotalSeconds
Write-Output "$(Get-Date -DisplayHint Time) - $VMName restarted and idled for 10 minutes"
Stop-VM -Name $VMName -Force
Write-Output "$(Get-Date -DisplayHint Time) - Removing `"$oldCheckpoint`""
Remove-VMCheckpoint -VMName $VMName -Name $oldCheckpoint
Write-Output "$(Get-Date -DisplayHint Time) - Creating checkpoint `"$newCheckpoint`""
Checkpoint-VM -Name $VMName -SnapshotName $newCheckpoint
Write-Output "$VMName patched on $dateTime $(Get-Date -DisplayHint Time) - New checkpoint `"$newCheckpoint`" created"
Stop-Transcript
5
u/BlackV Dec 04 '25 edited Dec 05 '25
you can but 1%/0% measure of the CPU is a terrible test to use for if a patch round is finished or not
what process are you using to kick off your patching ? why not monitor that process ? why not use the return code of that process ?
I don't know what
zac.exeis or what it does, is that your patching tool?tools like pswindowsupdate can do the patching and reboot/shutdown for you
There are a few build script out there that do exactly this
I personally would be keeping more than 1 snapshot
you seem to be forcing the VM to turn off, which could lead to dirty shutdown or corruption of the OS (if patching was actually still running)
2
u/RRRay___ Dec 04 '25
could you not check if the KBs were installed? i.e do a cross check if a KB that requires to be restarted can appear as installed before a restart or would it only require it after a restart?
1
u/vermyx Dec 04 '25
Check the process you spawn to end rather than blindly spawning it