Fixing a broken differencing disk with Virtual Server and VBScript

Differencing disks store a reference to their parent virtual hard disks.  If the parent virtual hard disk is moved, or renamed, the differencing disk will stop working - which will in turn stop any virtual machine that is using the differencing disk from starting up.  You can correct this problem through the Virtual Server user interface (by inspecting the differencing disk in question), or you can use a script like this:

 Option Explicit
  
 'Define constants
 CONST vmDiskType_Dynamic = 0
 CONST vmDiskType_FixedSize = 1
 CONST vmDiskType_Differencing = 2
  
 dim vs, aVHD, parentVHD, aVHDFileName, parentVHDFileName
  
 'Grab command line arguments
 aVHDFileName = WScript.Arguments.Item(0)
 parentVHDFileName = WScript.Arguments.Item(1)
  
 'Connect to Virtual Server
 Set vs = CreateObject("VirtualServer.Application")
  
 'Create VHD object
 set aVHD = vs.GetHardDisk(aVHDFileName)
 set parentVHD = vs.GetHardDisk(parentVHDFileName)
  
 'Merge the VHD to a new dynamic disk
 if aVHD.type = vmDiskType_Differencing then
    wscript.echo "Changing the parent virtual hard disk..."
    aVHD.Parent = parentVHD 
 end if

As you can see, this script takes two command-line parameters.  The first is the name of the differencing disk, and the second is the name of the new parent virtual hard disk (note that these both need to be full paths, and not relative paths.  e.g. "C:\foo\parent.vhd" not "..\..\foo\parent.vhd").  It then creates VHD COM objects for both, and sets the differencing disk to use the new parent disk.

Cheers,
Ben