The joys of Hyper-V and Windows Server 2008 R2 meant that you needed to shutdown the VM in order to let it merge the disks.

This became an issue for us, as we only allowed a small fixed size for the CSV on our Cluster. If we didn’t shut them down and merge the disks it would cause havoc for us.

My first iteration of this script was semi functional, however it relied on timeouts to occur before actually starting it up, and didn’t have any error Checking.

I have just completed with full testing the V2 of it, which completes the following steps:

CreateSnapshot – This includes creating the snapshot, then comparing Today’s Date with each snapshots date to ensure it worked successfully (this is where the TimeZoneDifference comes into play)

RemoveOldSnapshots – Deletes Snapshots older than 15 days

Invoke-VMShutdown – Shuts the VM down

MonitorSnapshotMerge – Monitors the Merge process and ensures that it completes

Start-VM – Starts the VM back up after successful merge

There is also error handling and logging, if an error is encountered it will throw an error in the log file and exit. If you still want it to move forward you will need to modify the script. I did not.

Logging is done to this log:

C:\Admin\Logs\HYPER-V\Hyper-V_Log.txt

You can download the script here:

Shutdown Hyper-V, Snapshot, Cleanup & Merge

(Updated to fix snapshot check issue)

You will need to change 2 variables in the script:

# name of the VM you want to shutdown & merge

$VM = “vmname”

# time zone difference, as snapshot creation time is in UTC and we need to convert it

$TimeZoneDifference = “10”