Skip to content

Instantly share code, notes, and snippets.

@PatrickTerlisten
Last active May 8, 2018 21:33
Show Gist options
  • Save PatrickTerlisten/0f54c49977b272589e4e to your computer and use it in GitHub Desktop.
Save PatrickTerlisten/0f54c49977b272589e4e to your computer and use it in GitHub Desktop.
This script outputs the pCPU/ vCPU ratio for each ESXi host.
#requires -Version 1
#requires -PSSnapin VMware.VimAutomation.Core
function Get-CpuRatio {
<#
.SYNOPSIS
No parameters needed. Just execute the script.
.DESCRIPTION
This script outputs the pCPU/ vCPU ratio for each ESXi host. Only the number of
physical cores is taken into account, not the number of logical processors
(physical cores + hyperthreading)
.EXAMPLE
Get-CpuRatio
Get the pCPU/ vCPU ratio for all ESXi hosts.
.NOTES
Author: Patrick Terlisten, [email protected], Twitter @PTerlisten
This script is provided "AS IS" with no warranty expressed or implied. Run at your own risk.
This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 4.0
International License (https://creativecommons.org/licenses/by-nc-sa/4.0/).
.LINK
http://www.vcloudnine.de
#>
# Create empty hash table
$OutputTable = @()
# Get a list of all ESXi Hosts
$VMHosts = Get-VMHost
# Get a list of all VMs
$VMs = Get-VM
try
{
# Loop through the ESXi hosts
ForEach ($VMHost in $VMHosts) {
# Init variable
$vCPUs = 0
$Ratio = 0
# Get the number of physical cores of the current ESXi host
$Cores = $VMHost.extensiondata.hardware.cpuinfo.NumCpuCores
# Summarize the number of vCPUs for all VMs on the current host
$VMs | Where-Object -FilterScript {$_.vmhost -like $VMHost} | ForEach-Object -Process {$vCPUs += $_.numcpu}
# If vCPU count is > 0, calculate the ratio of vCPUs to pCPUs
if ($vCPUs -ne '0') {$Ratio = "$('{0:N2}' -f ($vCPUs / $Cores))" + ':1'}
# Build Output
$Output = '' | Select-Object -Property Hostname, Cores, vCPUs, Ratio
$Output.Hostname += $VMHost.name
$Output.Cores += $Cores
$Output.vCPUs += $vCPUs
$Output.Ratio += $Ratio
# Fill $OutputTable
$OutputTable += $Output
}
}
catch
{
"Error was $_"
$line = $_.InvocationInfo.ScriptLineNumber
"Error was in Line $line"
}
finally
{
# Write OutputTable and sort by ratio
$OutputTable | Sort-Object -Property @{Expression = 'Ratio';Descending = $true} | Format-Table -AutoSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment