Hi,
I am unable to sort multiple objects from below script. Please help.
Get-VMHost | Select Name,
@{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},
@{N='CPU GHz Free %';E={[math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)),2)}},
@{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},
@{N='Memory Free %';E={[math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB)/ $_.MemoryTotalGB),2)*100}} | Sort -Property @{E="Name"; Ascending = $True}, @{E="CPU GHz Free %"; Descending = $true}, @{E= "Memory Free %"; Descending = $true} | ft -auto
You have to explicitly convert those 2 values to Float to have the numerical order.
Something like this
Get-VMHost | Select-Object Name,
@{N = 'CPU GHz Capacity'; E = { [math]::Round($_.CpuTotalMhz / 1000, 2) } },
@{N = 'CPU GHz Free %'; E = { [math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)), 2) } },
@{N = 'Memory Capacity GB'; E = { [math]::Round($_.MemoryTotalGB, 2) } },
@{N = 'Memory Free %'; E = { [math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB) / $_.MemoryTotalGB), 2) * 100 } } |
Sort-Object -Property @{E = 'Name'; Ascending = $True },
@{E = {[float]$_.'CPU GHz Free %'}; Descending = $true },
@{E = {[float]$_.'Memory Free %'}; Descending = $true } |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You have to explicitly convert those 2 values to Float to have the numerical order.
Something like this
Get-VMHost | Select-Object Name,
@{N = 'CPU GHz Capacity'; E = { [math]::Round($_.CpuTotalMhz / 1000, 2) } },
@{N = 'CPU GHz Free %'; E = { [math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)), 2) } },
@{N = 'Memory Capacity GB'; E = { [math]::Round($_.MemoryTotalGB, 2) } },
@{N = 'Memory Free %'; E = { [math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB) / $_.MemoryTotalGB), 2) * 100 } } |
Sort-Object -Property @{E = 'Name'; Ascending = $True },
@{E = {[float]$_.'CPU GHz Free %'}; Descending = $true },
@{E = {[float]$_.'Memory Free %'}; Descending = $true } |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Tried but it is sorting only the Name but it is not sorting the other objects.
That is not the output my snippet generates, you have more columns.
What code are you using?
Just tested my snippet again, and for me it is working
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Here is the output where I am unable to sort objects
I was put on the wrong foot as well.
The Sort-Object with multiple properties does the folowing
When you specify multiple properties, the objects are sorted by the first property. If multiple objects have the same value for the first property, those objects are sorted by the second property. This process continues until there are no more specified properties or no groups of objects.
So what you see is correct.
The lines are sorted on the Name, and only when there are entries with the same name will the next property be used.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh. Got it LucD.
Thanks for the detailed info.