Automation

 View Only
  • 1.  sort-object on %CPU not working

    Posted Dec 14, 2022 06:17 PM

    trying to get top usage on vms on a esx host with

     

    get-vmhost xxxx | get-vm | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, @{N="CPU Usage(%)"; E={ $_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1}}, @{N="Mem Usage(%)"; E={ $_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1}} | Sort-Object -Property "CPU Usage(%)" | ft -auto

     

     

    but its not putting the top % at the top. any idea?



  • 2.  RE: sort-object on %CPU not working

    Posted Dec 14, 2022 06:37 PM

    Use the Value in the calculated property, and use the -Descending switch

    Get-VMHost xxxx | Get-VM |
    Where {$_.PowerState -eq "PoweredOn"} |
    Select Name, Host, NumCpu, MemoryMB,
      @{N="CPU Usage(%)"; E={ ($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1).Value}},
      @{N="Mem Usage(%)"; E={ ($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1).Value}} |
    Sort-Object -Property "CPU Usage(%)" -Descending |
    Format-Table -AutoSize


  • 3.  RE: sort-object on %CPU not working

    Posted Dec 14, 2022 07:15 PM

    Hi Luc, When i try to use your script:

     

    $sStat = @{
    Entity = Get-VMHost -Name 'xxxxx' | Get-VM
    Stat = 'cpu.usage.average','mem.usage.average','disk.usage.average'
    Instance = ''
    MaxSamples = 1
    Realtime = $true
    ErrorAction = 'SilentlyContinue'
    }

    Get-Stat | Group-Object -Property {$_.Entity.Name} -PipelineVariable group |

    ForEach-Object -Process {

    New-Object -TypeName PSObject -Property (
    [ordered]@{
    VmName = $group.Name
    'Num CPUS' = (get-vm ($group.name)).numcpu
    MemoryGB = (get-vm ($group.name)).MemoryGB
    'CPU %' = $group.Group | where{$_.MetricId -eq 'cpu.usage.average'}
    'Memory %' = $group.Group | where{$_.MetricId -eq 'mem.usage.average'}
    'Disk' = $group.Group | where{$_.MetricId -eq 'disk.usage.average'}

    }

    )

    }| sort-object -Property "CPU %" -Descending | Format-Table -AutoSize

    it did not sort it based on highest cpu


  • 4.  RE: sort-object on %CPU not working

    Posted Dec 14, 2022 09:04 PM

    You didn't use the Value but the complete object returned by Get-Stat



  • 5.  RE: sort-object on %CPU not working

    Posted Dec 16, 2022 05:47 PM

    Having trouble understanding how to use group-object with the following:

     

    this works but want to use group-object and sort cpu from Top to bottom

     

    Get-VMHost xxxxx | Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, @{N="CPU Usage(%)"; E={ ($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1).Value}}, @{N="Mem Usage(%)"; E={ ($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1).Value}}, @{N="Disk"; E={ ($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddMinutes(-30) -Realtime -MaxSamples 1).Value}} | Sort-Object -Property "CPU Usage(%)" -Descending | ft -auto

     

    Thanks



  • 6.  RE: sort-object on %CPU not working

    Posted Dec 16, 2022 06:09 PM

    As I said in my previous reply, you have to use the Value property, not the complete object.
    Otherwise, the 'CPU %' property will not have the type Float, and sorting will be done on the String value, not the numerical value.

    $sStat = @{
      Entity = Get-VMHost -Name 'xxxxx' | Get-VM
      Stat = 'cpu.usage.average', 'mem.usage.average', 'disk.usage.average'
      Instance = ''
      MaxSamples = 1
      Realtime = $true
      ErrorAction = 'SilentlyContinue'
    }
    
    Get-Stat  |
    Group-Object -Property { $_.Entity.Name } -PipelineVariable group |
    ForEach-Object -Process {
      New-Object -TypeName PSObject -Property (
        [ordered]@{
          VmName = $group.Name
          'Num CPUS' = (Get-VM ($group.name)).numcpu
          MemoryGB = (Get-VM ($group.name)).MemoryGB
          'CPU %' = ($group.Group | Where-Object { $_.MetricId -eq 'cpu.usage.average' }).Value
          'Memory %' = ($group.Group | Where-Object { $_.MetricId -eq 'mem.usage.average' }).Value
          'Disk' = ($group.Group | Where-Object { $_.MetricId -eq 'disk.usage.average' }).Value
    
        }
      )
    } | Sort-Object -Property 'CPU %' -Descending |
    Format-Table -AutoSize

     



  • 7.  RE: sort-object on %CPU not working

    Posted Dec 16, 2022 06:15 PM

    makes sense. Thanks luc