在 PowerShell 中,如何提取 Note属性 的 属性(在一行中)?
In PowerShell, how do you extract a Property of a NoteProperty (in one line)?
我正在尝试使用 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 returns 什么都没有...添加 .Subject returns 什么都没有...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
对您的目的来说不是很有效,这个 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/函数返回的 class 的实例对象,这将允许您调用您的成员/属性和方法对象而不需要将它们存储在变量中。
这适用于您在 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。如果有什么不太清楚的地方请告诉我,不幸的是我不是最擅长“理论”的人。
我正在尝试使用 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 returns 什么都没有...添加 .Subject returns 什么都没有...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
对您的目的来说不是很有效,这个 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/函数返回的 class 的实例对象,这将允许您调用您的成员/属性和方法对象而不需要将它们存储在变量中。
这适用于您在 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。如果有什么不太清楚的地方请告诉我,不幸的是我不是最擅长“理论”的人。