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).
The process I followed to do this is:
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
List the incompatibilities and see that there are two virtual hard disks that have not been found.
Look at .Source.Path on each incompatibility to determine which virtual hard disk is missing
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"
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"
Finally use Import-VM with the corrected virtual machine compatibility report
Now the virtual machine is imported and ready to go.
Cheers,
Ben
Comments
- Anonymous
June 09, 2017
Thanks for the post, this helps but Set-VMHardDiskDrive doesn't work for VM which has a host based checkpoint in windows 2016. Can you try this particular scenario?