2 Replies Latest reply on Nov 13, 2019 4:49 AM by T180985

    get hosts with below average mem/cpu

    T180985 Enthusiast

      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?

        • 1. Re: get hosts with below average mem/cpu
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          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')

          }

          • 2. Re: get hosts with below average mem/cpu
            T180985 Enthusiast

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