Importing a VM with VHDs in Different Paths

I was recently given an interesting challenge.  I was asked to show how you could use PowerShell to import a virtual machine; where the virtual hard disks were stored in multiple different locations.  Now, if the virtual hard disks were all in a single location, you could just use the “VHDSourcePath” parameter on Import-VM to let Hyper-V know where to look.  But this does not work if the virtual hard disks are in multiple different locations.

To handle this situation – you need to use Compare-VM (as I discussed here).

import vhds

The process I followed to do this is:

  1. Use Compare-VM to create a virtual machine compatibility report.  Like this:

    $vmReport = compare-vm "C:\Users\benja_000\Desktop\Exported VM\Import Test VM\Virtual Machines\CCAE6F7E-B9FD-474B-9D08-A7011940C217.XML" -Copy –GenerateNewId

  2. List the incompatibilities and see that there are two virtual hard disks that have not been found.

  3. Look at .Source.Path on each incompatibility to determine which virtual hard disk is missing

  4. Use Set-VMHardDiskDrive to correct the location for the first virtual hard disk:

    Set-VMHardDiskDrive $vmReport.Incompatibilities[0].Source -Path "C:\Users\benja_000\Desktop\Exported VM\Import Test VM\Virtual Hard Disks\a\Import Test VM.vhdx"

  5. Use Set-VMHardDiskDrive to correct the location for the second virtual hard disk:

    Set-VMHardDiskDrive $vmReport.Incompatibilities[1].Source -Path "C:\Users\benja_000\Desktop\Exported VM\Import Test VM\Virtual Hard Disks\b\Another VHDX.VHDX"

  6. Finally use Import-VM with the corrected virtual machine compatibility report

Now the virtual machine is imported and ready to go.

Cheers,
Ben