PowerCLI

 View Only
  • 1.  Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 14, 2015 04:41 PM

    HI

    All of the following code is yielding the desired results except the last line. I can't seem to get it to work. What am I doing wrong?

    Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}},

                               @{N= "ESX Host";E={Get-VMHost -VM $_}},

                               @{N= "NumCPU";E={Get-VMHost -VM $_ | Select NumCPU}},

                               @{N= "Cores";E={Get-VMHost -VM $_ | Get-View).Hardware.CpuInfo.NumCpuCores}}

    Thanks!



  • 2.  RE: Get VM Cluster Name, Host Name, CPU and Cores
    Best Answer

    Posted Aug 14, 2015 06:41 PM

    There was a missing ( in front of your last Get-VMHost. You also just want the attribute NumCPU, not an object containing one attribute NumCPU so I've changed line 3 a little

    Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}},

        @{N= "ESX Host";E={Get-VMHost -VM $_}},

        @{N= "NumCPU";E={(Get-VMHost -VM $_).NumCPU}},

        @{N= "Cores";E={(Get-VMHost -VM $_ | Get-View).Hardware.CpuInfo.NumCpuCores}}

    If you're running this for a large # of VMs you may want to look at optimizing it a bit. Rather than calling "Get-VMHost" multiple times you can restructure a bit so its only called once

    Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}},

        @{N= "ESX Host";E={ $Script:VMH = (Get-VMHost -VM $_ ); $Script:VMH.Name }},

        @{N= "NumCPU";E={$Script:VMH.NumCPU}},

        @{N= "Cores";E={$Script:VMH.ExtensionData.Hardware.CpuInfo.NumCpuCores}}

    The 2nd piece of code is a little over 2x faster than the first since Get-VMHost isn't being called multiple times

    One other quick thing, if you're looking for the number of CPUs (i.e. how many sockets) then replace $Script:VMH.NumCPU with $Script:VMH.ExtensionData.Hardware.CpuInfo.NumCpuPackages; the NumCPU attribute of the host object is actually the number of cores so that can be a bit misleading



  • 3.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 14, 2015 06:58 PM

    Thanks Craig! Exactly what I was looking for and more.



  • 4.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 14, 2015 07:08 PM

    One more question if I may?

    How can I add the VMHost count?

    Thanks



  • 5.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 06:26 AM

    $Results = Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}},

        @{N= "ESX Host";E={ $Script:VMH = (Get-VMHost -VM $_ ); $Script:VMH.Name }},

        @{N= "NumCPU";E={$Script:VMH.NumCPU}},

        @{N= "Cores";E={$Script:VMH.ExtensionData.Hardware.CpuInfo.NumCpuCores}}

    # VM to host

    $Results

    # hosts per cluster (count)

    $Results | Select-Object Cluster, "ESX Host" -Unique | Group-Object Cluster -NoElement | Select-Object @{N="Cluster"; E={$_.Name}}, @{N="NumHosts"; E={$_.Count}}

    # total hosts

    "Total Hosts: " + ($Results | Select-Object "ESX Host" -Unique | Measure-Object).Count



  • 6.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 05:04 PM

    How would you pipe this all to a csv file?



  • 7.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 05:59 PM

    Each line in a CSV file needs to have the same fields, and you're dealing with three types of data (VM data, a host rollup, and a summation) so the fields aren't the same. What are you planning to do with the CSV file? There may be another format that could be used...



  • 8.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 06:03 PM

    I need to send this as a report. Your first answer worked and I was able to export all the data to a csv file. Now they want the vmhost cluster count in the report as well.

    Thanks for your quick response.



  • 9.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 06:26 PM

    You could do something similar to this (replace C:\reports with your preferred reports directory). You'll end up with 2 files that you can then import into whatever you're using to format your reports.


    $Results = Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}},

        @{N= "ESX Host";E={ $Script:VMH = (Get-VMHost -VM $_ ); $Script:VMH.Name }},

        @{N= "NumCPU";E={$Script:VMH.NumCPU}},

        @{N= "Cores";E={$Script:VMH.ExtensionData.Hardware.CpuInfo.NumCpuCores}}

    # VM to host

    $Results | Export-CSV -NoTypeInformation -Path "C:\Reports\VMs.csv"

    # hosts per cluster (count)

    $Results | Select-Object Cluster, "ESX Host" -Unique | Group-Object Cluster -NoElement | Select-Object @{N="Cluster"; E={$_.Name}}, @{N="NumHosts"; E={$_.Count}} | Export-CSV -NoTypeInformation -Path "C:\Reports\HostsByCluster.csv"





  • 10.  RE: Get VM Cluster Name, Host Name, CPU and Cores

    Posted Aug 17, 2015 06:53 PM

    I was hoping to have all the data in one file but oh well.

    Thanks again for your help!