[英]powershell object sort descending
this powershell stuff 这个PowerShell的东西
$Processes = get-process | Group-Object -Property ProcessName
foreach($Process in $Processes)
{
$Obj = New-Object psobject
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
$Obj | Add-Member -MemberType NoteProperty -Name Mem -Value ($Process.Group|Measure-Object WorkingSet -Sum).Sum
$Obj | sort Mem -Descending
}
outputs the same as this 输出与此相同
$Processes = get-process | Group-Object -Property ProcessName
foreach($Process in $Processes)
{
$Obj = New-Object psobject
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
$Obj | Add-Member -MemberType NoteProperty -Name Mem -Value ($Process.Group|Measure-Object WorkingSet -Sum).Sum
$Obj
}
I'm not well enough versed to know if it is in fact working but because the items are summed then it was ordered off of the first value, but because it is still sorted alphabetically I think it just isn't set to the correct value to sort. 我不太了解该功能是否确实有效,但是因为对这些项目进行了汇总,然后才从第一个值开始对其进行排序,但是由于它仍按字母顺序排序,因此我认为它的设置不正确排序。 I have tried these in several different combinations
我已经尝试了几种不同的组合
sort, sort-object, sort-object -property "Mem" -Descending, Mem, "Mem", WS, "WS", WorkingSet, @{Expression="Mem"; sort,sort-object,sort-object属性“ Mem”-降序,Mem,“ Mem”,WS,“ WS”,WorkingSet,@ {Expression =“ Mem”; Descending=$true} and various permutations,
Descending = $ true}和各种排列,
throwing the resulting $Obj to another sorted $Obj(that threw an error saying it didn't have addition $ObjS += $obj |sort etc) several other methods of calling sort on object that I didn't save or remember. 将生成的$ Obj扔给另一个排序的$ Obj(这引发了一个错误,说它没有添加$ ObjS + = $ obj | sort等)其他几种我没有保存或记住的对象上调用sort的方法。
and have come to the conclusion that my error likely stems from someplace else however because of no errors being thrown I believe that my syntax is correct at least. 并且得出的结论是我的错误可能是由于其他地方引起的,但是由于没有抛出任何错误,我相信我的语法至少是正确的。
I'd like the output to be sorted by the memory usage of the processes (combined by same name to get total memory of similar processes, 我希望根据进程的内存使用情况对输出进行排序(按相同名称组合以获取类似进程的总内存,
ie all of chromes processes as just one --chrome 1650453708-- 即所有的镀铬过程都只是一个--chrome 1650453708--
also this is on whatever powershell is with windows 7 if that helps at all 这也可以在Windows 7的任何Powershell上使用,如果有帮助的话
Here's a hint: What are you sorting here? 提示:您在这里排序什么?
foreach($Process in $Processes)
{
#...
$Obj | sort Mem -Descending
}
Build the data set then sort. 建立数据集然后排序。
Function Get-ProcessMemorySummary1
{
$Processes = Get-Process | Group-Object -Property ProcessName
foreach($Process in $Processes)
{
$Obj = New-Object psobject
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
$Obj | Add-Member -MemberType NoteProperty -Name Mem -Value ($Process.Group `
| Measure-Object WorkingSet -Sum).Sum
$Obj | sort Mem -Descending
}
}
Get-ProcessMemorySummary1 | Sort-Object Mem
Function Get-ProcessMemorySummary2
{
$Processes = get-process | Group-Object -Property ProcessName
foreach($Process in $Processes)
{
$Obj = New-Object psobject
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
$Obj | Add-Member -MemberType NoteProperty -Name Mem -Value ($Process.Group `
| Measure-Object WorkingSet -Sum).Sum
$Obj
}
}
Get-ProcessMemorySummary2 | Sort-Object Mem
Name Mem
---- ---
Idle 8192
smss 1277952
NisSrv 1536000
ptim 1765376
ptsrv 1945600
ONENOTEM 3014656
rundll32 3084288
Secure System 3899392
ibtsiva 4325376
SynTPHelper 4648960
fdlauncher 5128192
ssh-agent 5353472
ibmpmsvc 5521408
fdhost 6725632
...
Get-ProcessMemorySummary2 | Sort-Object Mem -Descending
Name Mem
---- ---
iexplore 1890992128
svchost 1115009024
powershell_ise 834617344
RDCMan 734556160
sqlservr 698155008
Microsoft.Photos 396951552
dwm 346951680
MsMpEng 201469952
explorer 184778752
...
Someone showed me a while back that using Add-Member is a resource hog. 不久前有人告诉我,使用Add-Member是一种资源浪费。 Using a PSCustomObject gives you potentially tighter code with less redundancy and allows for easy to read sorting.
使用PSCustomObject可以使您的代码更紧密,冗余度更低,并使排序易于阅读。
Get-Process | Group-Object -Property ProcessName | ForEach-Object {
[array]$objProcesses += [PSCustomObject][ordered] @{
Name = $_.Name
Mem = $(($_.Group | Measure-Object WorkingSet -Sum).Sum)
}
}
Return $objProcesses
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.