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?
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
Hi Luc, When i try to use your script:
You didn't use the Value but the complete object returned by Get-Stat
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
makes sense. Thanks luc