Fixing Hyper-V virtual machine import with Compare-VM

Well, I was rearranging some stuff the other day, and come to an interesting “lesson learned”, which I’ll share. 🙂

In my lab, I’ve had a Hyper-V server running Windows 2012 R2, which I finally wanted to upgrade to something newer. I’ve decided to go with the latest Windows Server Insider Preview (SA 20180), just for fun.

When trying to do an in-place upgrade, I was presented with the message “it can’t be done“, which is fine – my existing installation is with GUI, the new one will be Core.

So, evacuate everything and reinstall.

In the process, I’ve also reorganized some stuff (machines were moved to another disk, not all files were on the same place, etc.).

Installed Windows, installed Hyper-V, created VM switches, but when I tried to import it all back (from PowerShell… because I had no GUI anymore), I was presented with an error.

Error during virtual machine import was (I know – could’ve used more specific Import-VM command, which will select all the right folders and required options, but… learned something new by doing it this way!):

Import-VM -Path 'D:\VMs\azshci\azshci1\Virtual Machines\5381F80D-C752-42E0-AE26-6402B019B785.vmcx'
# Import-VM : Unable to import virtual machine due to configuration errors.  Please use Compare-VM to repair the virtual machine.
# At line:1 char:1
# + Import-VM -Path 'D:\VMs\azshci\azshci1\Virtual Machines\5381F80D-C752-42E0-AE26-6402B019B785.vmcx'
# + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#     + CategoryInfo          : InvalidOperation: (:) [Import-VM], VirtualizationException
#     + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.ImportVM

So, the error says it all – “Please use Compare-VM to repair the virtual machine.” 🙂

But how?! 🙂

If you go to the docs page of Compare-VM, you can see how it’s used.

And, in my case, the whole process of repairing this virtual machine looks like this:

# create an incompatibility report for a virtual machine
$report = Compare-VM -Path 'D:\VMs\azshci\azshci1\Virtual Machines\5381F80D-C752-42E0-AE26-6402B019B785.vmcx'

# check the created report as some incompatibilities were found
$report
# VM                 : VirtualMachine (Name = 'azshci1') [Id = '5381F80D-C752-42E0-AE26-6402B019B785']
# OperationType      : ImportVirtualMachine
# ...
# Incompatibilities  : {40010, 40010, 40010, 40010}
# ...

# check what exactly is incompatible (VHD locations, in this case)
$report.Incompatibilities.Source | Format-Table
# VMName  ControllerType ControllerNumber ControllerLocation DiskNumber Path
# ------  -------------- ---------------- ------------------ ---------- ----
# azshci1 SCSI           0                0                             C:\VMs\azshci\azshci1\azshci1_0_0.vhdx
# azshci1 SCSI           1                0                             C:\VMs\azshci\azshci1\azshci1_1_0.vhdx
# azshci1 SCSI           1                1                             C:\VMs\azshci\azshci1\azshci1_1_1.vhdx
# azshci1 SCSI           1                2                             C:\VMs\azshci\azshci1\azshci1_1_2.vhdx

# fixing the VHD locations (by replacing C: with D: for all VHDs)
$report.Incompatibilities[0].Source | Set-VMHardDiskDrive -Path 'D:\VMs\azshci\azshci1\azshci1_0_0.vhdx'
$report.Incompatibilities[1].Source | Set-VMHardDiskDrive -Path 'D:\VMs\azshci\azshci1\azshci1_1_0.vhdx'
$report.Incompatibilities[2].Source | Set-VMHardDiskDrive -Path 'D:\VMs\azshci\azshci1\azshci1_1_1.vhdx'
$report.Incompatibilities[3].Source | Set-VMHardDiskDrive -Path 'D:\VMs\azshci\azshci1\azshci1_1_2.vhdx'

# recheck if all looks fine
$report.Incompatibilities.Source | Format-Table
# VMName  ControllerType ControllerNumber ControllerLocation DiskNumber Path
# ------  -------------- ---------------- ------------------ ---------- ----
# azshci1 SCSI           0                0                             D:\VMs\azshci\azshci1\azshci1_0_0.vhdx
# azshci1 SCSI           1                0                             D:\VMs\azshci\azshci1\azshci1_1_0.vhdx
# azshci1 SCSI           1                1                             D:\VMs\azshci\azshci1\azshci1_1_1.vhdx
# azshci1 SCSI           1                2                             D:\VMs\azshci\azshci1\azshci1_1_2.vhdx

# import the fixed virtual machine (success!)
Import-VM -CompatibilityReport $report
# Name    State CPUUsage(%) MemoryAssigned(M) Uptime   Status             Version
# ----    ----- ----------- ----------------- ------   ------             -------
# azshci1 Off   0           0                 00:00:00 Operating normally 9.0

Hope this helps you as well!

Cheers!