Change a virtual machines boot order [Hyper-V Script]

Change a virtual machines boot order [Hyper-V Script]

Rate This
  • Comments 1


If you need to change the boot order of a virtual machine programmatically (so that it will boot off of the network first – for example) it is relatively straight forward:

# Function for handling WMI jobs / return values
Function ProcessResult($result, $successString, $failureString)
{
   #Return success if the return value is "0"
   if ($result.ReturnValue -eq 0)
      {write-host $successString} 
 
   #If the return value is not "0" or "4096" then the operation failed
   ElseIf ($result.ReturnValue -ne 4096)
      {write-host $failureString "  Error value:" $result.ReturnValue}
 
   Else
      {#Get the job object
      $job=[WMI]$result.job
 
      #Provide updates if the jobstate is "3" (starting) or "4" (running)
      while ($job.JobState -eq 3 -or $job.JobState -eq 4)
         {write-host $job.PercentComplete "% complete"
          start-sleep 1
 
          #Refresh the job object
          $job=[WMI]$result.job}
 
       #A jobstate of "7" means success
       if ($job.JobState -eq 7)
          {write-host $successString}
       Else
          {write-host $failureString
          write-host "ErrorCode:" $job.ErrorCode
          write-host "ErrorDescription" $job.ErrorDescription}
       }
}
 
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
 
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
 
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
 
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
 
# SettingType = 3 ensures that we do not get snapshots
$SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3}
 
# Change the boot order.  This is an array of four entries.
# The first entry in the array is the first boot choice.
# The second is the second, and so on.  The values map as follows:
#
# 0 == Floppy
# 1 == CD
# 2 == IDE
# 3 == Net
#
# The command below sets up the boot order as:
# Network, CD, IDE, Floppy
$SystemSettingData.BootOrder = @(3,1,2,0)
 
# Commit the changes
$result = $VMMS.ModifyVirtualSystem($VM, $SystemSettingData.GetText(1))
 
# Check to see that it all worked
ProcessResult $result "BIOS settings have been updated." "Failed to update BIOS settings."

Cheers,
Ben

Attachment: ChangeBootOrder.zip
Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • Hi Ben,

    Great blog - Thanks!

    I need to configure bios settings from inside the virtual machine itself without connecting through the Network to its own physical (hyperV) server...

    Do you know how I could manage that?

    Best,

    Paul

Page 1 of 1 (1 items)