This one proved trickier than I first thought. The total number of cores (sockets multiplied by cores per socket) is easy enough to get with get-vm – but it doesn’t tell you anything about the number of virtual sockets or virtual cores per socket assigned.
In case you don’t know what I’m talking about, shown here, when you edit the vCPUs of a VM:
I was told the need for this was application licensing related – which is abnormal – as most other licensing requirements are usually based on the physical sockets and cores of the host. Still, it was an interesting challenge.
Run this up in PowerCLI, after you’ve already used Connect-VIserver to connect to your vCenter server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Get all VMs $vms = get-vm; # Loop through each VM found foreach ($vm in $vms) { # Get VM details $vmname = $vm.name; $totalcores = [string]$vm.numcpu $corespersocket = [string]((get-view $vm).config.hardware.numcorespersocket); $sockets = [string]([int]($totalcores / $corespersocket)); $powerstate = [string]$vm.powerstate; # Build CSV output $out = $vmname + "," + $totalcores + "," + $sockets + "," + $corespersocket + "," + $powerstate; # Output to file $out >> c:\temp\vms.csv; } |
The resulting CSV file should then look like this (I added column headings in Excel):
Hi Kamal,
I’m looking for similar kind of ps to get vCPU/cores output for a particular VM for upto last 1 year in vSphere 5.5 environment.
Can you help get one? I tried few but cores output is none.
Thank you,
Digvijay
How do you add a line for the host that manages the vm and also the guest VMs operating system?
A little trickier – but not impossible.
Before you perform get-VM, perform a get-VMHost to get all hosts, then loop through the VMs on a per host basis, like this:
For the guest operating system, it depends on whether you want the actual OS, or what it’s configured as (on the VMware side of things).
For the actual OS, you could use get-WMIObject to query the win32_operatingsystem class (assuming it’s Windows VMs your looking at).
If you want to know what your host thinks the OS is, when you do get-VM there is a property called “Guest” that you can refer to.
EG: $vm.guest will return something like “DC01:Microsoft Windows Server 2016 (64-bit)”.
(DC01 is my server name in that example)
Hi Kamal,
Do you have a similar script for MS Windows 2012 R2?
Regards,
Larry
Hi Larry – do you mean for VMs running on Hyper-V? Or just to query any Windows server for CPU information?