使用Select-Object在PowerShell中过滤子对象
你们中的许多人都熟悉PowerShell中可能的计算属性,以从亚物体级别提高所需的属性。
即,如果我想知道一个流程的所有者(即记事本)作为一个非admim,我可以使用WMI和查询所有者对象进行此过程,
Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" |
Select-Object -Property ProcessId,Name,@{label='User'; expression={$_.GetOwner().User}}
ProcessId Name User
--------- ---- ----
16028 notepad.exe user01
1972 notepad.exe user01
我可以使用更多的属性作为domain
@{l='Domain'; e={$_.GetOwner().Domain}}
ProcessId Name Domain
--------- ---- ------
16028 notepad.exe domain01
1972 notepad.exe domain01
我还可以选择按原样保留所有者对象,
@{l='Owner'; e={$_.GetOwner()}}
ProcessId Name Owner
--------- ---- -----
16028 notepad.exe System.Management.ManagementBaseObject
1972 notepad.exe System.Management.ManagementBaseObject
但是,我该如何保持对象结构,而仅获得选择的子对象?
ProcessId Name Owner
--------- ---- -----
16028 notepad.exe @{User=user01; Domain=domain01}
1972 notepad.exe @{User=user01; Domain=domain01}
Many of you are familiar with the calculated properties possible in PowerShell, to raise a property needed from a sub-object level.
I.e. if I'd like to know the owner of a process (i.e. notepad) as a non-admim I could fetch the process with WMI and query for the owner object using
Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" |
Select-Object -Property ProcessId,Name,@{label='User'; expression={$_.GetOwner().User}}
ProcessId Name User
--------- ---- ----
16028 notepad.exe user01
1972 notepad.exe user01
I could proceed with more properties as Domain
@{l='Domain'; e={$_.GetOwner().Domain}}
ProcessId Name Domain
--------- ---- ------
16028 notepad.exe domain01
1972 notepad.exe domain01
I could also opt to keep the Owner object as is, if I need
@{l='Owner'; e={$_.GetOwner()}}
ProcessId Name Owner
--------- ---- -----
16028 notepad.exe System.Management.ManagementBaseObject
1972 notepad.exe System.Management.ManagementBaseObject
However, how do I go about to keep the object structure while only getting select subobjects?
ProcessId Name Owner
--------- ---- -----
16028 notepad.exe @{User=user01; Domain=domain01}
1972 notepad.exe @{User=user01; Domain=domain01}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案(PowerShell通常是这种情况)真的很简单。计算的属性使用代码块
{my-Code}
可以包含任何有效命令。因此,只需在表达式中使用select-object
。The answer (which is often the case with PowerShell) is really simple. Calculated properties uses a code block
{my-code}
that can contain any valid commands. So just useSelect-Object
within the expression.