Hyper-V

Nice to know: optimize-vhd in Windows Server 2012

I needed to compress a bunch of VHD’s and of course that means PowerShell so I did a (very simple) script for it. Its easy to use, just remember to set execution policy correctly before you use it otherwise the script does not run. So here it is


Param
(
[parameter(mandatory=$true,HelpMessage="Please, Give a folder to start from, ok?")][ValidateNotNullOrEmpty()]$VHDPath,
[parameter(mandatory=$true,HelpMessage="Please, Give the mode, quick or full?")][ValidateNotNullOrEmpty()]$Mode
)
get-childitem $VHDPath -recurse -filter *.vhd | ForEach-Object {optimize-vhd -Path $_.FullName -Mode $Mode}

The script takes to parameters, –VHDPath and –Mode
  • The VHDPath is the starting point from where to start search for VHD and VHDx files in the form of –VHDPath C:\VMs
  • The Mode is also simple to use, it takes Full, Quick, Retrim, Pretrimmed and Prezeroed. Now that does require some understanding I believe. So here is the explanation from TechNet:

Specifies the mode in which the virtual hard disk is to be optimized. For a VHD disk, the default mode is Full. For a VHDX disk, the default mode is Quick. Retrim – sends down retrims without scanning for zero blocks or reclaiming unused blocks. (Allowable only if the virtual hard disk is mounted read-only.) Quick – reclaims unused blocks, but does not scan for zero blocks. (Allowable only if the virtual hard disk is mounted read-only.) Full – scans for zero blocks and reclaims unused blocks. (Allowable only if the virtual hard disk is mounted read-only.) Pretrimmed – performs as Quick mode, but does not require the virtual hard disk to be mounted read-only. The unused space detection will be less effective than quick mode where the virtual hard disk had been mounted read-only as the scan will be unable to query information about free space in the NTFS file system within the virtual hard disk. Useful when the VHDX has been used by OS instances which are Win8 or greater or the VHDX has already been the target of this cmdlet in Retrim mode. Prezeroed – performs as Quick mode, but does not require the virtual hard disk to be mounted read-only. The unused space detection will be less effective than if the virtual hard disk had been mounted read-only as the scan will be unable to query information about free space in the NTFS file system within the virtual hard disk. Useful if a tool was run previously to zero all the free space on the virtual disk as this mode of compaction can then reclaim that space for subsequent block allocations. This form of compaction can also be useful in handling virtual hard disk containing file systems other than NTFS.

/mike

Categories: Hyper-V

Tagged as: ,

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.