In my Hyper-V environment I have a test domain which I use for pretty much all of my training, writing and video work. As part of my "belt and suspenders" approach, I periodically take a snapshot of all the virtual machines using the theory that if I had to, I could roll back the entire domain.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Today I deleted the old snapshot and was planning on firing off a new one. As I was getting ready to run this command:
get-vm chi* | checkpoint-vm -snapshotname "Safety Snapshot"
I realized disks were still merging from the snapshot deletion I had just done, and that I should wait until this completed. Of course, I didn't want to constantly run Get-VM to check the status. So I built a simple background job to loop while merging. I used this command:
while (get-vm chi* | where status -eq "merging disks") { write-host "." -nonewline start-sleep -seconds 1 }
As long as the Get-VM expression is true, PowerShell will run through the scriptblock. In this case I'm writing a . to indicate progress and then sleeping for 1 second. I probably should have gone 5 or 10 seconds. Anyway, this now runs as a background job. I can periodically check on the job. Or use Wait-Job.
Once I see that the job has completed, I can create my snapshots. And because that too might take time to complete, I'll use the -AsJob parameter for Checkpoint-VM. I suppose I could have added the command in my waiting job. And maybe next time I will. What I really need to do is write a script to clear the old snapshot, wait for the merge to complete and then take a new snapshot. I suppose it might look like this, although I haven't tested it yet.
#requires -version 3.0 #requires -module Hyper-V #remove existing snapshots Write-Host "Removing existing snapshots" -ForegroundColor Green Get-VMSnapshot -name "safety snapshot" -VMName CHI* | Remove-VMSnapshot #wait for disk merges to complete Write-Host "Waiting for disks to merge" -ForegroundColor Green while (Get-VM chi* | where status -eq "merging disks") { Write-Host "." -nonewline -ForegroundColor green Start-Sleep -seconds 10 } #take new snapshots Write-Host "Taking new snapshot" -ForegroundColor Green Get-VM -Name CHI* | Checkpoint-VM -SnapshotName "Safety Snapshot" Write-Host "Script complete." -ForegroundColor Green
I would run the entire script as a background job. Actually I might even take this a step further and run this as a scheduled PowerShell job so I don't have to think about this anymore.
2 thoughts on “Hyper-V Waiting to Merge”
Comments are closed.