Return object from array with highest value

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

I want to return an object from an array who's property has the highest value. Currently I am doing the following

Get-VM | Sort-Object -Property ProvisionedSpaceGB | Select-Object -Last 1 

This works but is inefficient. I don't need the entire array sorted, I just need the object with largest value. Ideally I would use something like

Get-VM | Measure-Object -Property ProvisionedSpaceGB -Maximum 

but this only returns the value of the object property, not the entire object. Is there a way to have measure-object return the base object?

回答1:

Not directly. Measure-Object is intended to be an easy way to grab such values, not their input objects. You could get the maximum from Measure-Object and then compare against the array, but it takes a few steps:

$array = Get-VM $max = ($array | measure-object -Property ProvisionedSpaceGB -maximum).maximum $array | ? { $_.ProvisionedSpaceGB -eq $max} 

You could also forgo Measure-Object entirely and iterate through the set, replacing the maximum and output as you go.

$max = 0 $array | Foreach-Object  {      if($max -le $_.ProvisionedSpaceGB)     {          $output = $_          $max = $_.ProvisionedSpaceGB     }  } $output 

This is a little dirtier so as to always return a single value. It would need a minor adjustment if you were to reuse it in a case where there may be multiple values that have the same maximum (filesize lengths when using Get-ChildItem, for example). It will replace $output with the latter iterate in a case where two or more objects have the same value for ProvisionedSpaceGB. You could turn $output into a collection easily enough to fix that.

I prefer the former solution myself, but I wanted to offer a different way to think about the problem.



回答2:

You can use this:

$array = Get-VM | Sort-Object  -Property ProvisionedSpaceGB -Descending $array[0] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!