I have stumbled upon this today and like seeminlgy everyone else the original solution did not provide data for all clusters. Since I have access to the hosts’s current usage metrics in the vmhost objects I decided to skip the stats and read them out directly on the hosts and average them across the cluster.
(sorry to bump this old thread, but since Google lead me here I thought I might as well post my solution here)
Here is the code (copy & pasting it on the iPad so it might no look pretty) :
$clusters
= get-cluster$myClusters = @()
foreach ($cluster in $clusters) {
$hosts = $cluster |get-vmhost
[double]$cpuAverage = 0
[double]$memAverage = 0
Write-Host $cluster
foreach ($esx in $hosts) {
Write-Host $esx
[double]$esxiCPUavg = [double]($esx | Select-Object @{N = 'cpuAvg'; E = {[double]([math]::Round(($_.CpuUsageMhz) / ($_.CpuTotalMhz) * 100, 2))}} |Select-Object -ExpandProperty cpuAvg)
$cpuAverage = $cpuAverage + $esxiCPUavg
[double]$esxiMEMavg = [double]($esx | Select-Object @{N = 'memAvg'; E = {[double]([math]::Round(($_.MemoryUsageMB) / ($_.MemoryTotalMB) * 100, 2))}} |select-object -ExpandProperty memAvg)
$memAverage = $memAverage + $esxiMEMavg
}
$cpuAverage = [math]::Round(($cpuAverage / ($hosts.count) ), 1)
$memAverage = [math]::Round(($memAverage / ($hosts.count) ), 1)
$ClusterInfo = "" | Select-Object Name, CPUAvg, MEMAvg
$ClusterInfo.Name = $cluster.Name
$ClusterInfo.CPUAvg = $cpuAverage
$ClusterInfo.MEMAvg = $memAverage
$myClusters += $ClusterInfo
}
$myClusters
Edit: Updated my script to be multi-vcenter compatible. Old version used VMhost ID which is not unique across vCenters. The new approach gets the vmhost object from the cluster object which proved to work with multiple vCenter servers connected at the same time when running this script.
Here is a sample output of the script:
