VMware Cloud Community
T180985
Expert
Expert
Jump to solution

get hosts with below average mem/cpu

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?

Please mark helpful or correct if my answer resolved your issue. How to post effectively on VMTN https://communities.vmware.com/people/daphnissov/blog/2018/12/05/how-to-ask-for-help-on-tech-forums
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

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

  (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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

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

  (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

0 Kudos
T180985
Expert
Expert
Jump to solution

I spend all morning trying to get that working and you figure it out in 30 seconds :smileycry: Thanks!!!

Please mark helpful or correct if my answer resolved your issue. How to post effectively on VMTN https://communities.vmware.com/people/daphnissov/blog/2018/12/05/how-to-ask-for-help-on-tech-forums
0 Kudos