I have the below function which correctly pulls all hosts that have below average CPU usage and then below average Mem Usage. The issue im having is with taking both of those lists and displaying a separate list with only hosts that are both below average for both metrics.
function WhichHost2 {
$esx = Get-datacenter $global:DCChoice | Get-Cluster $global:CLUChoice | Get-VMHost
$memstats = Get-Stat -Entity $esx -Stat "mem.usage.average" -Realtime -MaxSamples 1
$memavg = $memstats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$cpustats = Get-Stat -Entity $esx -Realtime -MaxSamples 1 | Where {$_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq ""}
$cpuavg = $cpustats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$BelAvgMemStats = $memstats | where{$_.Value -lt $memavg} | Select -ExpandProperty Entity
$BelAvgCPUStats = $cpustats | where{$_.Value -lt $cpuavg} | Select -ExpandProperty Entity
Write-Host
Write-Host "Below Average Memory" -ForegroundColor green
$BelAvgMemStats
Write-Host
Write-Host "Below Average CPU" -ForegroundColor green
Write-Host
$BelAvgCPUStats
write-host
Write-Host "All Below Average" -ForegroundColor green
Write-Host
$BelAvgMemStats | where {$_.Name -Match $BelAvgCPUStats.Name}
write-host
Write-Host -NoNewLine "Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
If i run $BelAvgMemStats.Name i get a list of VMhost names, if i run $BelAvgCPUStats.Name likewise i get a list of VMhost names. Even though some match, i dont get any responses back? Ive tried using Compare-Object but i didnt have much luck with that either... any thoughts?
Try something like this
{
$esx = Get-Datacenter $global:DCChoice | Get-Cluster $global:CLUChoice | Get-VMHost
$memstats = Get-Stat -Entity $esx -Stat "mem.usage.average" -Realtime -MaxSamples 1
$memavg = $memstats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$cpustats = Get-Stat -Entity $esx -Realtime -MaxSamples 1 | Where { $_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq "" }
$cpuavg = $cpustats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$BelAvgMemStats = $memstats | where { $_.Value -lt $memavg } | Select -ExpandProperty Entity
$BelAvgCPUStats = $cpustats | where { $_.Value -lt $cpuavg } | Select -ExpandProperty Entity
Write-Host
Write-Host "Below Average Memory" -ForegroundColor green
$BelAvgMemStats
Write-Host
Write-Host "Below Average CPU" -ForegroundColor green
Write-Host
$BelAvgCPUStats
write-host
Write-Host "All Below Average" -ForegroundColor green
Write-Host
(Compare-Object -ReferenceObject $BelAvgMemStats -DifferenceObject $BelAvgCPUStats -ExcludeDifferent -IncludeEqual).InputObject.Name
Write-Host -NoNewLine "Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
{
$esx = Get-Datacenter $global:DCChoice | Get-Cluster $global:CLUChoice | Get-VMHost
$memstats = Get-Stat -Entity $esx -Stat "mem.usage.average" -Realtime -MaxSamples 1
$memavg = $memstats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$cpustats = Get-Stat -Entity $esx -Realtime -MaxSamples 1 | Where { $_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq "" }
$cpuavg = $cpustats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$BelAvgMemStats = $memstats | where { $_.Value -lt $memavg } | Select -ExpandProperty Entity
$BelAvgCPUStats = $cpustats | where { $_.Value -lt $cpuavg } | Select -ExpandProperty Entity
Write-Host
Write-Host "Below Average Memory" -ForegroundColor green
$BelAvgMemStats
Write-Host
Write-Host "Below Average CPU" -ForegroundColor green
Write-Host
$BelAvgCPUStats
write-host
Write-Host "All Below Average" -ForegroundColor green
Write-Host
(Compare-Object -ReferenceObject $BelAvgMemStats -DifferenceObject $BelAvgCPUStats -ExcludeDifferent -IncludeEqual).InputObject.Name
Write-Host -NoNewLine "Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I spend all morning trying to get that working and you figure it out in 30 seconds :smileycry: Thanks!!!