使用 Select-Object 在非默认属性之间指定

Specifiying between non default properties with Select-Object

我正在练习Powershell,分配给我的问题如下:

编写管道以获取进程,其中 cpu 利用率大于零,return 属性未显示在默认视图中,并按 CPU 对结果进行排序降序

目前我认为我已经完成了除非默认属性要求之外的所有内容,我认为这可以通过 Select-Object cmdlet 完成。

我目前的代码:

Get-Process | Where-Object {$_.CPU -gt 0 } | Select-Object -Property * | sort -Descending

我知道星号是选择所有属性的名字,但我不知道如何设置布尔检查 属性 是否在默认视图中。有人可以帮帮我吗?

查看Select-Object中的-ExcludeProperty选项:

Get-Process | `
Where-Object {$_.CPU -gt 0 } | `
Select-Object -Property * -ExcludeProperty $(Get-Process)[0].PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | `
sort -Descending | format-table

其中:

$(Get-Process)[0].PSStandardMembers.DefaultDisplayPropertySet).ReferencedPropertyNames

Get-Process 输出中第一个返回值的默认列表。分解整个事情:

# First process
$fp = $(Get-Process)[0]
# Standard Members
$PSS = $fp.PSStandardMembers
# Default Display Property Set
$DDPS = $PSS.DefaultDisplayPropertySet
# Names of those properties in a list, that you can pass to -ExcludeProperty
$Excl = $DDPS.ReferencedPropertyNames
$Excl

# Command using variables
Get-Process | `
Where-Object {$_.CPU -gt 0 } | `
Select-Object -Property * -ExcludeProperty $Excl | `
sort -Descending | format-table