PowerShell 技能连载 - 对象的魔法(第 3 部分)

假设您希望隐藏对象所有没有值(为空)的属性。以下是一个简单的实现:

1
2
3
4
5
6
# get any object
$object = Get-Process -Id $pid

# try and access the PSObject
$propNames = $object.PSObject.Properties.Where{$null -ne $_.Value}.Name
$object | Select-Object -Property $propNames

这段代码将只输出包含值的属性。您甚至可以对属性排序:

1
2
3
4
5
6
# get any object
$object = Get-Process -Id $pid

# try and access the PSObject
$propNames = $object.PSObject.Properties.Where{$null -ne $_.Value}.Name | Sort-Object
$object | Select-Object -Property $propNames
评论