PowerCLI

 View Only
Expand all | Collapse all

Get Number of VM CPU and Cores

  • 1.  Get Number of VM CPU and Cores

    Posted Mar 14, 2016 06:30 PM

    Hi

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



  • 2.  RE: Get Number of VM CPU and Cores

    Posted Mar 15, 2016 06:21 AM

    Those lines should be.

    These are actual values, not MoRefs (pointers)

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

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



  • 3.  RE: Get Number of VM CPU and Cores

    Posted Mar 15, 2016 01:13 PM

    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



  • 4.  RE: Get Number of VM CPU and Cores

    Posted Mar 15, 2016 01:33 PM

    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

                }

            }

        }



  • 5.  RE: Get Number of VM CPU and Cores

    Posted Mar 15, 2016 02:36 PM

    HI Luc,

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



  • 6.  RE: Get Number of VM CPU and Cores
    Best Answer

    Posted Mar 15, 2016 03:03 PM

    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

                }

            }

        }



  • 7.  RE: Get Number of VM CPU and Cores

    Posted Mar 15, 2016 03:27 PM

    That was it. Thanks!



  • 8.  RE: Get Number of VM CPU and Cores

    Posted Feb 01, 2018 12:33 AM
    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?


  • 9.  RE: Get Number of VM CPU and Cores

    Posted Feb 01, 2018 05:33 AM

    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



  • 10.  RE: Get Number of VM CPU and Cores

    Posted Feb 01, 2018 04:08 PM

    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!



  • 11.  RE: Get Number of VM CPU and Cores

    Posted Feb 06, 2019 07:16 AM

    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



  • 12.  RE: Get Number of VM CPU and Cores

    Posted Feb 06, 2019 08:09 AM

    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



  • 13.  RE: Get Number of VM CPU and Cores

    Posted Feb 06, 2019 12:05 PM

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

     

    Cheers,

    Roberto



  • 14.  RE: Get Number of VM CPU and Cores

    Posted May 07, 2019 02:27 PM

    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|


  • 15.  RE: Get Number of VM CPU and Cores

    Posted May 07, 2019 02:54 PM

    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



  • 16.  RE: Get Number of VM CPU and Cores

    Posted May 09, 2019 12:20 PM
    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.