Converting Virtual Hard Disks programmatically

Normally when people ask me for a specific VBScript, I just do a web search and point them to the most promising looking result.  The other day I was asked for a script to convert a dynamic virtual hard disk to a fixed size virtual hard disk – and was surprised to find that no one had already published one.  So for the sake of posterity:

set vsApp = CreateObject(“VirtualServer.Application”,”localhost”)

TargetVHDpath =Inputbox(“Enter path and name of VHD to convert:”)
FixedVHDpath =Inputbox(“Enter path and name of VHD to create”)

set target = vsApp.GetHardDisk(TargetVHDpath)

set convertTask = target.convert(FixedVHDpath,1)

while not convertTask.isComplete
   wscript.echo “Conversion is ” & convertTask.PercentCompleted & “% complete”
   WScript.Sleep 2000

wscript.echo “Conversion complete”

Two things to note:

  1. Like all of my VBScripts I am assuming that you use CSCRIPT.EXE to run your scripts.  If you use WSCRIPT you should pull out the line that shows percentage completion – as this will be just annoying.

  2. This script converts a dynamic disk to a fixed disk.  If you wanted to go the other way around – you just need to change the ‘1’ to a ‘0’ in the .convert() call.