Changing Hyper-V Processor Limit with PoSH

Like many companies today, my company is now using Hyper-V and Microsoft Virtual Server 2008 as a virtual hosting solution. We recently migrated from Virtual Server 2005 to Virtual Server 2008 with Hyper-V.

We quickly learned that when we created or deployed new VM’s they ran terribly slow. After some pondering and thinking back to the original version of Virtual Server, I recalled the resource allocation features. However, I could not find the ability to adjust the resources in the VMM 2008 application; you manage the resource allocation from the Hyper-V manager. We had already discovered that there were things you could do in the VMM that you couldn’t do in the Hyper-V manager and vice-versa. I’m sure this is by design, but what a pain in the [ahem].

Using the Hyper-V manager, I found that the values were set so the virtual machines were all using 12%. I will have to admit, I’ve not had ample time to thoroughly research the “whys” behind this peculiarity. We manage our VMs by setting quota points of 1 per virtual. Each “standard” VM owner is only allowed 1 quota point at a time; each “power” VM owner is allowed 2. This ensures that the VM owners use the VMM Portal managing their own VM’s and keeps too many VM’s from being deployed at once. What we have found is that the popularity and flexibily of VM’s is increasing the number of VM’s being hosted! Setting the resource limit to 100% on all VM’s on a densely populated host is probably not the best idea. But, if they are managed well and VMs are not deployed when not in use, it provides the best performance and encourages the use of VM’s in an organization.

To adjust the resources on a machine to improve performance, we changed the virtual machine limit under the processor settings to 100%. Since we use PowerShell quite regularly in deploying VM’s, installing software to VM’s, as well as other tasks, we tossed around the idea of doing the same to adjust this setting.

After a bit of playing around using get-member and exporing the classes for the virtualization namespace of WMI, I began to piece a simple script together. I found Dung Hoang’s blog quite helpful in speeding up my script creation. Many kudos to Dung Hoang and his great blog on exploring Hyper-V with PoSH. You can find his blog here: http://dungkhoang.spaces.live.com/blog/.

The script below is what I am currently using to change the processor limit. Admittedly, it’s not polished yet. However, it will give you a good idea of what I’m doing with the script.

$HostServer = Read-Host "Enter the Hyper-V Host Server Name"
$VirtualElement = Read-Host "Enter the Virtual Machine Name"</pre>
$VSService = get-wmiobject -computerName $HostServer –namespace root\virtualization –class Msvm_VirtualSystemManagementService
$VM = Get-WmiObject -computerName $HostServer -namespace root\virtualization Msvm_ComputerSystem | where {$_.ElementName -match $VirtualElement}
$VMProcessorSettings = get-wmiobject -computerName $HostServer –namespace root\virtualization –class Msvm_ProcessorSettingData | where {$_.InstanceId -match $VM.Name }
if ($VMProcessorSettings.Limit -ne 100000){
# Set value to maximum
$VMProcessorSettings.Limit = 100000
$VMNewProcessorSetting = $VMProcessorSettings.psbase.GetText()
$result = $VSService.ModifyVirtualSystemResources($VM.__PATH, $VMNewProcessorSetting)
if ($result.ReturnValue -eq 0) {
Write-Host "Success"
} else {
Write-Host "Failure, return code: $result "
}

}
$result.Job
# Error Handling
trap{
Write-Host "ERROR: script execution was terminated.`n" $_.Exception.Message
exit
}
Advertisements