There are times where I want to load a live cd on a Virtual Machine or I wish to change the boot order to a 2nd HDD for some testing/troubleshooting purposes. I got tired of restarting the virtual machines into the BIOS to change the boot order, so here is some code that should help you if you are in the same boat as I was.

You’ll want to change the vm within the $vm variable as well as decide which devices you want booting first on the BootOrder line. The example below will boot from CD first before trying to boot the 1st HDD.

Note* If you have more than one cd-drive on a virtual machine, the bios will boot from the first virtual CD-rom drive with bootable content, so plan accordingly. When I deploy an unattended Windows install I place the Windows.iso on the 2nd CD-rom drive so that a batch file can mount VM-tools to the 1st after bootup.

$vm = get-vm "MGMT-VM"
if ($vm.ExtensionData.Summary.runtime.powerstate -eq 'PoweredOn') {
$vm | shutdown-vmguest -confirm:$false
$VMName = get-vm "$vm" | get-view
$HDD1Key = ($VMName.Config.Hardware.Device | ?{$_.DeviceInfo.Label -eq "Hard Disk 1"}).Key
if(($VMName.Config.Hardware.Device | ?{$_.DeviceInfo.Label -eq "Hard Disk 2"}).Key){
Write-Host "2nd HDD found"
$HDD2Key =($VMName.Config.Hardware.Device | ?{$_.DeviceInfo.Label -eq "Hard Disk 2"}).Key } Else {Write-Host "No 2nd HDD found"}
$bootHDD1 = New-Object -TypeName VMware.Vim.VirtualMachineBootOptionsBootableDiskDevice -Property @{"DeviceKey" = $HDD1Key}
$bootHDD2 = New-Object -TypeName VMware.Vim.VirtualMachineBootOptionsBootableDiskDevice -Property @{"DeviceKey" = $HDD2Key}
$BootCD = New-Object -Type VMware.Vim.VirtualMachineBootOptionsBootableCdromDevice

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec -Property @{

"BootOptions" = New-Object VMware.Vim.VirtualMachineBootOptions -Property @{

BootOrder = $BootCD, $BootHDD1, $BootHDD2
Write-Host "Boot Order Set"
if ($vm.ExtensionData.Summary.runtime.powerstate -eq 'PoweredOff'){ get-vm $vm | start-vm}