使用Select-Object在PowerShell中过滤子对象

发布于 2025-01-29 15:54:26 字数 1126 浏览 2 评论 0原文

你们中的许多人都熟悉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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-02-05 15:54:26

答案(PowerShell通常是这种情况)真的很简单。计算的属性使用代码块{my-Code}可以包含任何有效命令。因此,只需在表达式中使用select-object

Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" | 
  Select ProcessId,Name,@{l='Owner'; e={$_.GetOwner() | Select User,Domain}}

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 use Select-Object within the expression.

Get-WmiObject -ClassName Win32_Process -Filter "name='notepad.exe'" | 
  Select ProcessId,Name,@{l='Owner'; e={$_.GetOwner() | Select User,Domain}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文