VMware Cloud Community
tdubb123
Expert
Expert

sort-object on %CPU not working

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?

0 Kudos
6 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

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 @SStat | 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
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

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

0 Kudos
LucD
Leadership
Leadership

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 @SStat |
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

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

makes sense. Thanks luc

0 Kudos