VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Get Number of VM CPU and Cores

Hi

I've tried the following but cannot get the number of VM CPU and Cores. What am I doing wrong?

pastedImage_0.png

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$myCol = @()

foreach ($cluster in Get-Cluster)

    {

        foreach($vmhost in ($cluster | Get-VMHost))

        {

            foreach($vm in (Get-VM -Location $vmhost)){

                $VMView = $vm | Get-View

                $VMSummary = "" | Select ClusterName,HostName,VMName,VMSockets,VMCores,CPUSockets,CPUCores

                $VMSummary.ClusterName = $cluster.Name

                $VMSummary.HostName = $vmhost.Name

                $VMSummary.VMName = $vm.Name

                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu

                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket

                $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages

                $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores

                $myCol += $VMSummary

            }

        }

    }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

Those lines should be.

These are actual values, not MoRefs (pointers)

$vmSummary.VMSockets = $vm.Config.Hardware.NumCPU

$vmSummary.VMCores = $vm.Config.Hardware.NumCoresPerSocket


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

For some reason this did not work:

$vmSummary.VMSockets = $vm.Config.Hardware.NumCPU

$vmSummary.VMCores = $vm.Config.Hardware.NumCoresPerSocket

This worked:

$vmSummary.VMSockets = $vm.NumCPU

but this didn't:

$vmSummary.VMCores = $vm.NumCoresPerSocket

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that should be the $vmview variable instead of the $vm variable.

Like this

$myCol = @()

foreach ($cluster in Get-Cluster)

    {

        foreach($vmhost in ($cluster | Get-VMHost))

        {

            foreach($vm in (Get-VM -Location $vmhost)){

                $VMView = $vm | Get-View

                $VMSummary = "" | Select ClusterName,HostName,VMName,VMSockets,VMCores,CPUSockets,CPUCores

                $VMSummary.ClusterName = $cluster.Name

                $VMSummary.HostName = $vmhost.Name

                $VMSummary.VMName = $vm.Name

                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu

                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket

                $myCol += $VMSummary

            }

        }

    }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

HI Luc,

Isn't it possible to get the Sockets and cores for both the hosts and VMs?

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$myCol = @()

foreach ($cluster in Get-Cluster)

    {

        foreach($vmhost in ($cluster | Get-VMHost))

        {

            foreach($vm in (Get-VM -Location $vmhost)){

                $VMView = $vm | Get-View

                $VMSummary = "" | Select ClusterName,HostName,VMName,VMSockets,VMCores,CPUSockets,CPUCores

                $VMSummary.ClusterName = $cluster.Name

                $VMSummary.HostName = $vmhost.Name

                $VMSummary.VMName = $vm.Name

                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu

                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket

                $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages

                $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores

                $myCol += $VMSummary

            }

        }

    }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

That was it. Thanks!

Reply
0 Kudos
nixnerd
Contributor
Contributor
Jump to solution

This is very nice.  But I need to get this info for Guest OS that are Redhat only.  We have various OS within our VMware environments.  Any ideas?  Somewhere in the Get-View area? or 2nd For loop?
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have VMware Tools installed, you can do

$myCol = @()

foreach ($cluster in Get-Cluster)

    {

        foreach($vmhost in ($cluster | Get-VMHost))

        {

            foreach($vm in (Get-VM -Location $vmhost | where{$_.Guest.OSFullName -match "Red Hat"})){

                $VMView = $vm | Get-View

                $VMSummary = "" | Select ClusterName,HostName,VMName,OS,VMSockets,VMCores,CPUSockets,CPUCores

                $VMSummary.ClusterName = $cluster.Name

                $VMSummary.HostName = $vmhost.Name

                $VMSummary.VMName = $vm.Name

                $VMSummary.OS = $vm.Guest.OSFullName

                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu

                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket

                $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages

                $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores

                $myCol += $VMSummary

            }

        }

    }

$myCol


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nixnerd
Contributor
Contributor
Jump to solution

Thats awesome!  Thank you...  I did not know that info was available for Get-VM thought one had to use Get-View.  

Anywho, thanks again!

Reply
0 Kudos
RoArHa
Contributor
Contributor
Jump to solution

Hi,

I just found this nice script. I was wondering how to filter the vmsockets that are exceeding the cpusocket & maybe also in an nice format?

I am already happy with this script.

Many thanks in advance.

 

Cheers

Roberto

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can do something like this.

Not sure what you mean by the "nice format"

$myCol = @()

foreach ($cluster in Get-Cluster) {

   foreach ($vmhost in ($cluster | Get-VMHost)) {

   $vms = Get-VM -Location $vmhost|

   where {$_.ExtensionData.Config.Hardware.NumCpu -gt $_.vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages}

   foreach ($vm in $vms) {

   $VMView = $vm | Get-View

   $VMSummary = "" | Select ClusterName, HostName, VMName, VMSockets, VMCores, CPUSockets, CPUCores

   $VMSummary.ClusterName = $cluster.Name

   $VMSummary.HostName = $vmhost.Name

   $VMSummary.VMName = $vm.Name

   $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu

   $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket

   $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages

   $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores

   $myCol += $VMSummary

   }

   }

}

$myCol


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
RoArHa
Contributor
Contributor
Jump to solution

Thank you very much. You pointed me in the right direction.

 

Cheers,

Roberto

Reply
0 Kudos
PCAL
Contributor
Contributor
Jump to solution

I am getting the following error when trying to run this from both ISE and CLI 6.5R1 :

Exception has been thrown by the target of an invocation.
At H:\Scripts\VM_Sockets_Cores_Updated.ps1:6 char:11
+    $vms = Get-VM -Location $vmhost|

Exception has been thrown by the target of an invocation.
At H:\Scripts\VM_Sockets_Cores_Updated.ps1:6 char:11
+    $vms = Get-VM -Location $vmhost|
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach the script you are using as a file to this thread?
The Attach button is in the lower right part of the edit window


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
PCAL
Contributor
Contributor
Jump to solution

It seems that was just for an individual record, I ran it again with some patience and it completed successfully.  Thank you for all you do for this forum.
Reply
0 Kudos