0

I attempted to sort hosts using code that was similar to this (this may be slightly incorrect because I'm not currently at a box with PowerCLI installed):

$hosts = Get-VMHost
$SortedHosts = $hosts | Sort-Object CPUUsageMhz,MemoryUsageGB

The problem with this is that it is sorting it based on CPUUsageMhz first, then sorting it by MemoryUsageGB. I want to sort it so that the host with the overall lowest usage (Memory and CPU) is at the top/front of the list, and the most utilized host is at the bottom. An issue I ran into with my current sorting method is that a host with the lowest CPUUsageMhz had the highest MemoryUsageGB.

EGr
  • 2,072
  • 10
  • 41
  • 61

1 Answers1

1

Check out example 6 from the Sort-Object help.

get-childitem *.txt | sort-object -property @{Expression={$_.LastWriteTime - $_.CreationTime}; Ascending=$false}

This illustrates how to design a custom sorting algorithm, with independent Ascending preference.

You will need to design the algorithm yourself, as there is no standard way to sort a combination of CPU and RAM use. Depending on the complexity, you might need to resort to handling the sorting yourself, rather than using Sort-Object.

Good luck!

Cookie Monster
  • 1,741
  • 1
  • 19
  • 24
  • Thanks, I'll start trying to figure out an algorithm to sort them better. I wasn't sure if there was an easier way to do it since it's using powercli; but this should work. – EGr Mar 09 '14 at 19:27