Calculate vCPU to pCPU ratio for Hosts using PowerCLI

In some capacity planning scenarios, statistical information is not taken into account and the management of resources through best efforts can become a  rule of thumb scenario. In the case, of CPU resource the concept of vCPU to CPU ratio has been discussed as a possible method.

Using PowerCLI I was able to produce the ratio of vCPU to pCPU ratio by retrieving Host and VM information and combing this with math.

Firstly we will connect to the VI Server and create a collection of Hosts.

Connect-VIServer server1.domain.local
$VMHosts = Get-VMHost

For each host returned in the collection I will retrieve the number of vCPUs for all VMs on that particular host by returning the NumCPU of each VM, joining each value returned with a ‘+’ and invoking an expression agaisnt the string to produce the total number of vCPUS on the host.

ForEach ($VMHost in $VMHosts)
    $vCPU = Invoke-Expression ((Get-VMHost $VMHost.Name | Get-VM).NumCPU -join '+')

Now we will produce an informational message to show the vCPU to pCPU ratio, we can return the number of physical cores on the Host by returning the ‘Hardware.CPUInfo.NumCPUCores’ value and then dividing the total number of vCPUS on the Host by this value and use the Math.Round method to round the calculated value to one decimal place.

"The vCPU to pCPU ratio for Host '" + $VMHost.Name + "' is " + ([math]::round($vCPU / $VMHost.Hardware.CPUInfo.NumCpuCores,1)) + ":1"

Running the above will return the following output:

The vCPU to pCPU ratio for Host 'esxi1.domain.local' is 3:1
The vCPU to pCPU ratio for Host 'esxi2.domain.local' is 6.6:1
The vCPU to pCPU ratio for Host 'esxi3.domain.local' is 2.4:1
The vCPU to pCPU ratio for Host 'esxi4.domain.local' is 2.2:1

Whilst this will generate the current vCPU to pCPU ratio, this alone shouldn’t be used as a capacity planning/sizing method, from using management tools and/or retrieving  the statistical information (Get-Stat) for both Hosts and VMs you can generate a far more accurate picture of your environment.

The main reason for me generating the above was to get a quick overview whilst killing a bit of down time with PowerCLI.


3 thoughts on “Calculate vCPU to pCPU ratio for Hosts using PowerCLI

  1. The script doesn’t for me, more specifically the:

    ((Get-VMHost $VMHost.Name | Get-VM).NumCPU

    gives an error (something about an empty string):

    Invoke-Expression : Impossible de lier l’argument au paramètre « Command », car il s’agit d’une chaîne vide.
    Au niveau de ligne : 2 Caractère : 28
    + { $vCPU = Invoke-Expression <<<< ((Get-VMHost $VMHost.Name | Get-VM).NumCPU -join '+')
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand


    1. This would suggest that no host name is being passed to the cmdlet, if you was to manually connect to your vCenter server using PowerCLI and run the below do you return a list of all ESX(i) Hosts?

      $VMHosts = Get-VMHost


      1. I finally got things working and modified the script to use get-view:

        ForEach ( $VMHost in (Get-View -ViewType HostSystem) ) {
        $vcpu = invoke-expression ( (get-view -viewtype virtualmachine -searchroot $vmhost.moref|% {$_.config.hardware.numcpu}) -join ‘+’ )
        “The vCPU to pCPU ratio for Host ‘” + $VMHost.Name + “‘ is ” + ([math]::round($vCPU / $VMHost.Hardware.CPUInfo.NumCpuCores,1)) + “:1”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s