Converting Virtual Hard Disks programmatically

Converting Virtual Hard Disks programmatically

  • Comments 3

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
wend

wscript.echo
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.

Cheers,
Ben

Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
  • Was it you who also put it up on Technet[1]?

    [1] http://www.microsoft.com/technet/scriptcenter/csc/scripts/servers/virtual/cscsv042.mspx
  • Phylyp -

    No - it must have been someone else.

    Cheers,
    Ben
  • Is there no way to convert from Dynamic to Static in place, without creating a copy? I virtualized my C drive then installed a new OS, but trying to dual boot the old VHD errors out that there's not enough space, as the dynamic disk's maximum size exceeds the space remaining on the disk, so Windows won't boot to the drive?

    Any alternatives other than plugging in a large USB drive and converting to a new file on the USB, then deleting the old and copying back the new file? There should be a way to do the conversion in place.

Page 1 of 1 (3 items)