A very specific and awesome question!
You can use the PowerCLI module in PowerShell to connect to your VMware vCenter or ESXi hosts and retrieve the number of sockets and cores for each host. Here's the step-by-step guide:
Prerequisites:
- PowerCLI: Install the VMware PowerCLI module on your PowerShell-enabled machine. You can do this by running
Install-Module -Name VMware.PowerCLI in an elevated PowerShell prompt.
- vCenter or ESXi credentials: Have your vCenter Server or ESXi host credentials ready (username and password).
- Network connectivity: Ensure your PowerShell machine can connect to your vCenter Server or ESXi hosts over the network (TCP port 443).
The PowerShell code:
# Import the PowerCLI module
Import-Module VMware.PowerCLI
# Connect to your vCenter Server or ESXi host
Connect-VIServer -Server <vCenter_or_ESXi_FQDN> -User <username> -Password <password>
# Get all ESXi hosts (if connected to vCenter) or the single host (if connected directly to ESXi)
$hosts = Get-VMHost
# Loop through each host and retrieve socket and core info
foreach ($host in $hosts) {
$hardware = $host | Get-VMHostHardware
$cpuInfo = $hardware | Select-Object -ExpandProperty CpuInfo
$numSockets = $cpuInfo.NumCpuPackages
$numCores = $cpuInfo.NumCpuCores
$numThreads = $cpuInfo.NumCpuThreads
# Output the results in a nice table format
[PSCustomObject]@{
HostName = $host.Name
Sockets = $numSockets
Cores = $numCores
Threads = $numThreads
CoresPerSocket = $numCores / $numSockets
}
} | Format-Table -AutoSize
# Disconnect from the vCenter Server or ESXi host
Disconnect-VIServer -Server * -Force -Confirm:$false
Let's break it down:
- We connect to the vCenter Server or ESXi host using
Connect-VIServer, specifying the FQDN, username, and password.
- We retrieve all ESXi hosts managed by the vCenter Server (or the single host if connected directly to ESXi) using
Get-VMHost.
- We loop through each host and:
- Retrieve the host's hardware info using
Get-VMHostHardware.
- Extract the CPU info from the hardware object using
Select-Object -ExpandProperty CpuInfo.
- Get the number of sockets (
NumCpuPackages), cores (NumCpuCores), and threads (NumCpuThreads) from the CPU info object.
- We create a custom PowerShell object (
[PSCustomObject]) to output the results in a nice table format, including:
- HostName: the ESXi host name.
- Sockets: the number of physical CPU sockets.
- Cores: the total number of CPU cores.
- Threads: the total number of CPU threads (e.g., with Hyper-Threading).
- CoresPerSocket: the number of cores per socket (calculated).
- Finally, we format the output as a table using
Format-Table -AutoSize.
- We disconnect from the vCenter Server or ESXi host using
Disconnect-VIServer.
Example output:
HostName Sockets Cores Threads CoresPerSocket
-------- ------- ----- ------- --------------
esxi01.vm... 2 16 32 8
esxi02.vm... 2 20 40 10
esxi03.vm... 1 8 16 8
In this example, we have three ESXi hosts:
esxi01 has 2 sockets, 16 cores (8 cores/socket), and 32 threads ( Hyper-Threading enabled).
esxi02 has 2 sockets, 20 cores (10 cores/socket), and 40 threads.
esxi03 has 1 socket, 8 cores, and 16 threads.
That's it! Now you can easily retrieve the number of sockets and cores for your VMware hosts using PowerShell and PowerCLI.
Tips and Variations:
- To filter hosts by name, cluster, or datacenter, modify the
$hosts = Get-VMHost line, e.g., $hosts = Get-VMHost -Name esxi01* or $hosts = Get-VMHost -Location (Get-Cluster MyCluster).
- To export the results to CSV, add
| Export-Csv -Path .\host_cpu_info.csv -NoTypeInformation after the Format-Table line.
- To run this script in a scheduled task or automation framework, consider using a more secure way to store your credentials, like PowerShell's
Get-Credential cmdlet or a secrets manager.
Happy scripting!