12 Replies Latest reply on Feb 6, 2019 4:04 AM by RoArHa

    Get Number of VM CPU and Cores

    AGFlora Hot Shot

      Hi

       

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

       

       

       

       

        • 1. Re: Get Number of VM CPU and Cores
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Those lines should be.

          These are actual values, not MoRefs (pointers)

           

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

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

           

           

          • 2. Re: Get Number of VM CPU and Cores
            AGFlora Hot Shot

            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

             

             

            • 3. Re: Get Number of VM CPU and Cores
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              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

                          }

                      }

                  }

               

               

              • 4. Re: Get Number of VM CPU and Cores
                AGFlora Hot Shot

                HI Luc,

                 

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

                 

                • 5. Re: Get Number of VM CPU and Cores
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  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

                              }

                          }

                      }

                   

                   

                  • 6. Re: Get Number of VM CPU and Cores
                    AGFlora Hot Shot

                    That was it. Thanks!

                    • 7. Re: Get Number of VM CPU and Cores
                      nixnerd Lurker
                      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?
                      • 8. Re: Get Number of VM CPU and Cores
                        LucD Guru
                        User ModeratorsvExpertCommunity Warriors

                        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

                         

                        1 person found this helpful
                        • 9. Re: Get Number of VM CPU and Cores
                          nixnerd Lurker

                          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!

                          • 10. Re: Get Number of VM CPU and Cores
                            RoArHa Novice

                            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

                            • 11. Re: Get Number of VM CPU and Cores
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              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

                              • 12. Re: Get Number of VM CPU and Cores
                                RoArHa Novice

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

                                 

                                Cheers,

                                Roberto