【问题标题】:In PowerShell, how do you extract a Property of a NoteProperty (in one line)?在 PowerShell 中,如何提取 NoteProperty 的属性(一行)?
【发布时间】:2021-04-08 05:12:10
【问题描述】:

我正在尝试使用 Get-AuthenticodeSignature 提取签名者主题字符串。我想出了如何通过首先将 SignerCertificate NoteProperty 分配给一个变量,然后选择主题来获得它。但是,我终其一生都无法弄清楚如何将所有内容集中在一行中。

这行得通:

PS C:\> $SignerInfo = Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe"| Select-Object SignerCertificate

PS C:\> $SignerInfo
SignerCertificate
-----------------
[Subject]…

PS C:\> $SignerInfo.SignerCertificate
Thumbprint                                Subject              EnhancedKeyUsageList
----------                                -------              --------------------
A4341B9FD50FB9964283220A36A1EF6F6FAA7840  CN=Microsoft Window… {Windows System Component Verification, Code Signing}

PS C:\> $SignerInfo.SignerCertificate.Subject
CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

我试图把它放在一行中,这真的很接近:

PS C:\> Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe"| Select-Object SignerCertificate
SignerCertificate
-----------------
[Subject]…

但是我如何更深入地从这里提取主题?添加第二个 Select-Object 不会返回任何内容...添加 .Subject 不会返回任何内容... Get-Member 告诉我 SignerCertificate 是一个 NoteProperty,Select-Object * 会转储所有内容。

PS C:\> Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe"| Select-Object *
SignerCertificate      : [Subject]
                           CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
                         [Issuer]
                           CN=Microsoft Windows Production PCA 2011, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
                         [Serial Number]
                           3300000266BD1580EFA75CD6D3000000000266
etc...

我可以使用 ;让它像这样单行:$SignerInfo = Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe"; $SignerInfo.SignerCertificate.Subject,但现在是一个学习的时刻,要弄清楚打开 [Subject]… 到底是怎么回事

这些不起作用(尽管它们似乎应该起作用,因为它们在您使用变量时起作用):

Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe" | Select-Object SignerCertificate.Subject
Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe" | Select-Object SignerCertificate | Select-Object Subject

如何直接从管道中提取主题?

【问题讨论】:

  • 您是否只对元数据(主题、颁发者等值)感兴趣,还是需要导出证书本身?
  • 使用Select-Object 效率非常低(对不起,个人意见)。回答您的问题,您可以这样做:(Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe").SignerCertificate.Subject
  • @MathiasR.Jessen 就我个人而言,我只对主题感兴趣......但问题实际上是关于访问 SignerCertificate NoteProperty 中包含的任何数据的正确方法。我不明白为什么你有时可以索引它而不是其他的。
  • @SantiagoSquarzon - 您应该将此作为解决方案发布,这样我就可以为您提供一个好的答案。谢谢!
  • @vvildcard 你去吧,我添加了一些例子。希望它可以帮助您了解更多。

标签: powershell properties


【解决方案1】:

使用Select-Object 对您的目的不是很有效,这个cmdlet 在对数据集进行数据分析时非常有用,主要是因为它的参数-First、-Last、-Unique 等。

回答您的问题,以下是如何在一行中获取证书的 Subject 属性:

PS C:\> (Get-AuthenticodeSignature "C:\Windows\system32\notepad.exe").SignerCertificate.Subject
CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

通过使用( ) 封装您的 cmdlet,您正在创建 cmdlet/函数返回的类的实例对象,这将允许您调用对象的成员/属性和方法,而无需将它们存储在一个变量。

这适用于您在 PowerShell 上执行的所有操作,不仅是 cmdlet,还有一些示例:

# Calling the ToUniversalTime() method of the DateTime class
PS C:\> (Get-Date).ToUniversalTime()

Wednesday, April 7, 2021 8:59:59 PM

# Getting the last process and calling the Name property
PS C:\> (Get-Process)[-1].Name
YourPhone

# This is calling the TextInfo class out of our Get-Culture cmdlet and then using the .ToTitleCase() method of our TextInfo class
PS C:\> (Get-Culture).TextInfo.ToTitleCase('some random string')
Some Random String

当您不确定对象的成员和方法是什么时,您可以随时使用Get-Member

我希望这可以帮助您更多地了解 PowerShell 和 OOP。如果有什么不清楚的地方请告诉我,不幸的是我不是最擅长“理论”的人。

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多