06 February 2012

Change VM Boot Order via PowerShell

There was a recent question in the VMware PowerCLI community about changing the boot order for a VM (Change Boot Sequence with new Extensiondata option).  In looking into this, I found that the VirtualMachineBootOptions item has a BootOrder property with which one can change the VM's boot order (new in vSphere API 5.0).

Upon searching about to see if there were already examples of using this property for changing boot order, I found none.  There are places that talk about changing/limiting what boot devices a VM may use in order to boot from the given device, but none that dealt with changing the actual boot order on the VM.  So, I whipped out an example (that person was wanting to use the ExtensionData property of a VM object).

The BootOrder property can have four (4) different types of devices (which are VirtualMachineBootOptionsBootableDevice objects): CDRom, disk, ethernet, and floppy.

The CDRom boot device does not correspond to any particular CDRom on the VM -- per the docs, when the boot order specifies CDRom, the first CDRom with bootable media found is used.

The disk and ethernet boot devices are related to the actual VirtualDisk and VirtualEthernetCard devices on the VM by specifying the corresponding Keys for the given devices.

Here is an example of setting the boot order for a VM to NIC2, Disk1, CD:
## the VM to configure
$strVMName = "myVM0"
## the device name of the NIC to which to boot
$strBootNICDeviceName = "Network adapter 2"
## the device name of the hard disk to which to boot
$strBootHDiskDeviceName = "Hard disk 1"
## get the .NET View object for the VM, with a couple of select properties
$viewVM = Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device -Filter @{"Name" = "^$strVMName$"}

## get the VirtualEthernetCard device, and then grab its Key (DeviceKey, used later)
$intNICDeviceKey = ($viewVM.Config.Hardware.Device | ?{$_.DeviceInfo.Label -eq $strBootNICDeviceName}).Key
## bootable NIC BootOption device, for use in setting BootOrder (the corresponding VirtualEthernetCard device on the VM has PXE enabled, assumed)
$oBootableNIC = New-Object -TypeName VMware.Vim.VirtualMachineBootOptionsBootableEthernetDevice -Property @{"DeviceKey" = $intNICDeviceKey}

## get the VirtualDisk device, then grab its Key (DeviceKey, used later)
$intHDiskDeviceKey = ($viewVM.Config.Hardware.Device | ?{$_.DeviceInfo.Label -eq $strBootHDiskDeviceName}).Key
## bootable Disk BootOption device, for use in setting BootOrder (the corresponding VirtualDisk device is bootable, assumed)
$oBootableHDisk = New-Object -TypeName VMware.Vim.VirtualMachineBootOptionsBootableDiskDevice -Property @{"DeviceKey" = $intHDiskDeviceKey}

## bootable CDROM device (per the docs, the first CDROM with bootable media found is used)
$oBootableCDRom = New-Object -Type VMware.Vim.VirtualMachineBootOptionsBootableCdromDevice

## create the VirtualMachineConfigSpec with which to change the VM's boot order
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec -Property @{
    "BootOptions" = New-Object VMware.Vim.VirtualMachineBootOptions -Property @{
        ## set the boot order in the spec as desired
        BootOrder = $oBootableNIC, $oBootableHDisk, $oBootableCDRom
    } ## end new-object
} ## end new-object

## reconfig the VM to use the spec with the new BootOrder
$viewVM.ReconfigVM_Task($spec)

To display the VM's BIOS boot order after the update (not terribly obvious from the output):
PS C:\> ## get the updated View info about the given property
PS C:\> $viewVM.UpdateViewData("Config.BootOptions.BootOrder")
PS C:\> ## display the BootOrder
PS C:\> $viewVM.Config.BootOptions

BootDelay        :
EnterBIOSSetup   :
BootRetryEnabled :
BootRetryDelay   :
BootOrder        : {4001, 2000, VMware.Vim.VirtualMachineBootOptionsBootableCdromDevice}
DynamicType      :
DynamicProperty  :

...where 4001 is the DeviceKey of the second virtual NIC in the VM, and 2000 is the DeviceKey for the first virtual disk.

Original BIOS Boot settings:

BIOS Boot settings after updating them with the script:

Notes about setting the boot order in this manner:  once you have done so on a VM, it appears that the boot order can only be changed via script.  Notice in the first screenshot that one may use keys to navigate between items, whereas the second screenshot states that "All items on this menu cannot be modified in user mode". Something to keep in mind.  Additionally, this method does not seem to have control over where the "Removable Devices" item appears in the boot order.

Enjoy!