Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Hyper-V Waiting to Merge

Posted on August 28, 2013

Microsoft Hyper-VIn 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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Hyper-V Waiting to Merge”

  1. Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #44 - Flo's Datacenter Report
  2. Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #44 | Eskchi

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d