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!
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
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
Thanks Craig! Exactly what I was looking for and more.
One more question if I may?
How can I add the VMHost count?
Thanks
$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
How would you pipe this all to a csv file?
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...
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.
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"
I was hoping to have all the data in one file but oh well.
Thanks again for your help!