A while ago I did a number of posts talking about the different controls that are available to you in regards to how Hyper-V schedules virtual machine processor resources (Hyper-V CPU Scheduling–Part 1, Hyper-V CPU Scheduling–Part 2, Hyper-V CPU Scheduling–Part 3 and Hyper-V CPU Scheduling–Part 4).  Today I want to share a PowerShell script that shows you how to configure these settings programmatically:

# 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}
 
# Get the processor setting data
$ProcSetting = $SystemSettingData.getRelated("Msvm_ProcessorSettingData") | select -first 1
 
# Get new values from the user
$NewReservation = Read-Host "Specify the CPU reserve (from 0-10000) [Currently:"$ProcSetting.Reservation"]"
$NewLimit = Read-Host "Specify the CPU limit (from 0-10000) [Currently:"$ProcSetting.Limit"]"
$NewWeight = Read-Host "Specify the CPU weight (from 0-10000) [Currently:"$ProcSetting.weight"]"
 
# Update ProcSetting with the new values
$ProcSetting.Reservation = $NewReservation
$ProcSetting.Limit = $NewLimit
$ProcSetting.weight = $NewWeight
 
# Apply the changes to the processor setting data back to the virtual machine
$result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))
 
# Process the result
ProcessResult $result "Updated processor scheduling settings." "Failed to update processor scheduling settings."

Cheers,
Ben