Reimage a Hyper V Gen 2 / UEFI VM

hyper-vuefi

So we finally bit the bullet and started using UEFI for our desktops. It works perfectly for our Hyper V images – we can create gen 2 VMs all day long. This is true until we need to reimage. How do I set a Gen 2 VM with an existing OS to boot from PXE?

For the life of me I can't change the boot order to allow me to boot from network. In the firmware interface under system settings it only lists the windows boot managers. It's painful and slow to have to remove and recreate the VMs each time we want to try a new variation of an image.. How can we tackle this? I'm guessing there's some buried flag in the VM I need to edit with Powershell, but the documentation is lacking.

Best Answer

I am having the same issues/problems as Tim Brigham. That you (Grigory) do have the options, does not mean we do have them. Here is the screenshot of the boot options I have in the VM: Missing boot options Holding down shift doesn't work as you describe either. This does seem to work.

How to change the boot order (NIC first):
I have done some research and it seems that you can only change the boot order for Generation 2 VM's, running on Windows Server 2012 R2 or later, using powershell. I used the following powershell commands to change the boot order:

$VM = Get-VM "VmName"
$network = $VM | Get-VMFirmware |select -ExpandProperty BootOrder | where {$_.FirmwarePath.EndsWith("MAC(000000000000)")}
$VM | Set-VMFirmware -FirstBootDevice $network

Now the VM will boot from the network. Afterwards you probably need/want to change the boot order back to file being the first boot device. You could also choose to keep the network as the first boot device.

Update 15-3-2017
Heads-up warning
Every time you re-image an UEFI machine, an extra 'bootmgfw.efi' will be added to the firmware list. Removing the obsolete bootmgfw files is not a straightforward process; you'll need to use BCDEDIT in the VM itself to clean them up. A guide on how to do this can be found here. When using this guide, be careful not the remove the GUIDs linked to the EFI boot devices

How to change the boot order (File first):
If you have removed the obsolete firmware entries using the guide mentioned above, you can change the boot order back to file being first using the following powershell code:

$VM = Get-VM "VmName"
$file = $VM | Get-VMFirmware |select -ExpandProperty BootOrder | where {$_.FirmwarePath.EndsWith(".efi")}
$VM | Set-VMFirmware -FirstBootDevice $file

Note: I find it weird that the firmware for Gen2 VM's running on windows 2012 R2 and up only show the 'file' option. I am wondering: is this by design or is it a bug?