Powershell script to merge Hyper-V Virtual Machine disks


In some situations you might find a VM disk to be part of a series of differencing disks. Take this example:

Merge4

If this was the result of VM Checkpoints, the fix would be easy: simply delete the checkpoints which merges the disks. But his is not the case. Hyper-V Manager shows no checkpoints:

Merge9

Powershell confirms (notice no output):

Merge10

This script shuts down the VM and merges its disks. It uses the Get-ParentPath function.

This script can be downloaded from the Microsoft Script Center Repository.

To use this script, download it, run it as administrator, then use the function.

To see help:

Merge11

For example:
Merge-VMDisks -VMName ‘MyVM’
Merge1
This may take some time depending on disk sizes and underlying storage system speed.
Merge3
The script leaves a log file shows the same steps on the console.
This is the picture after the merge is done:
Merge5
Merge6
If the script is run on a VM that has no disks to be merged, no changes are made:
Merge8
The script prompts for confirmation before shutting down the VM:
Merge7
This can be bypassed by using the -Confirm parameter as shown above.
Script help output looks like:
NAME
    Merge-VMDisks
    
SYNOPSIS
    Function to merge VM disks
    
SYNTAX
    Merge-VMDisks [-VMName]  [[-LogFile] ] [-WhatIf] [-Confirm] []
    
DESCRIPTION
    Function/script to merge VM disks. The script will power down the VM in the process.

PARAMETERS
    -VMName 
        Name of the VM whose VHD(x) disks are to be merged
        
        Required?                    true
        Position?                    1
        Default value                
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
        
    -LogFile 
        Name and path of the file where the script will log its steps and progress
        
        Required?                    false
        Position?                    2
        Default value                ".\Merge-VMDisks_$(Get-Date -format yyyyMMdd_hhmmsstt).txt"
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -WhatIf []
        
        Required?                    false
        Position?                    named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -Confirm []
        
        Required?                    false
        Position?                    named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see 
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 
    
NOTES
    
        Function by Sam Boutros
        v1.0 - 11/01/2014
    
    -------------------------- EXAMPLE 1 --------------------------
    
    C:\PS>Merge-VMDisks -VMName 'v-2012R2-VBR1'
    
RELATED LINKS
    https://superwidgets.wordpress.com/category/powershell/

Advertisements

One response

  1. Pingback: Running Powershell script functions | Sam's Corner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s