如何在选择对象中选择名称/值对?

要检查程序集的数字签名,我使用Windows PowerShell Get-AuthenticodeSignature

Get-ChildItem -File -Path "C:\Program Files (x86)\My Company\My Product\Components\microsoft.*.dll" -Recurse |
    Get-AuthenticodeSignature |
    Select-Object -Property Path,Status,@{Name='SubjectName';Expression={($_.SignerCertificate.Subject)}}

输出字段SubjectName的名称/值对过多:

Path                                                                                                    Status SubjectName
----                                                                                                    ------ -----------
C:\Program Files (x86)\My Company\My Product\Components\microsoft.Expression.Drawing.dll             NotSigned
C:\Program Files (x86)\My Company\My Product\Components\microsoft.Expression.Interactions.dll        NotSigned
C:\Program Files (x86)\My Company\My Product\Components\microsoft.ReportViewer.Common.dll                Valid CN=microsoft Corporation,OU=MOPR,O=microsoft Corporation,L=Redmond,S=Washington,C=US
C:\Program Files (x86)\My Company\My Product\Components\microsoft.ReportViewer.DataVisualization.dll     Valid CN=microsoft Corporation,C=US

如何将输出减小为单个值:“ microsoft Corporation”? 我想要的输出是这样的CSV:

Path,SubjectName
C:\Program Files (x86)\My Company\My Product\Components\microsoft.Expression.Drawing.dll,NotSigned,C:\Program Files (x86)\My Company\My Product\Components\microsoft.Expression.Interactions.dll,C:\Program Files (x86)\My Company\My Product\Components\microsoft.ReportViewer.Common.dll,Valid,microsoft Corporation
C:\Program Files (x86)\My Company\My Product\Components\microsoft.ReportViewer.DataVisualization.dll,microsoft Corporation
GoDolphinboy 回答:如何在选择对象中选择名称/值对?

我将使用一个小的辅助函数来从X500DistinghuishedName中解析发行者名称:

function Get-IssuerName([string]$IssuerDN) {
    # helper function to parse the name out of the X500DistinghuishedName formatted 'Issuer' string
    if ($IssuerDN -match 'O=([^,]+)') { $matches[1] }
    elseif ($IssuerDN -match 'CN=([^,]+)')  { $matches[1] }
    else { $IssuerDN }
}

然后,在您的Select语句中使用它,如下所示:

Select-Object -Property Path,Status,@{Name='SubjectName';Expression={(Get-IssuerName $_.SignerCertificate.Subject)}}
,

在计算所得的属性中删除主题中不需要的部分:

@{n='SubjectName',e={$_.SignerCertificate.Subject -replace ',.*' -replace '^CN='}}
,

总结:

  1. 使用function Get-IssuerName
  2. 使用Get-IssuerName语句中的Select-Object执行脚本行
  3. 将输出放入Export-Csv

这是脚本:

function Get-IssuerName([string]$IssuerDN) {
    # helper function to parse the name out of the X500DistinghuishedName formatted 'Issuer' string
    if ($IssuerDN -match 'O=([^,]+)')  { $matches[1] }
    else { $IssuerDN }
}

Get-ChildItem -File -Path "C:\Program Files (x86)\My Company\My Product\Components\Microsoft.*.dll" -Recurse |
    Get-AuthenticodeSignature |
    Select-Object -Property Path,@{Name='SubjectName';Expression={(Get-IssuerName $_.SignerCertificate.Subject)}} |
        Export-Csv -Path d:\temp\AuthenticodeSignature.csv -NoTypeInformation

PowerShell

以及生成的CSV输出:

"Path","Status","SubjectName"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.Expression.Drawing.dll","NotSigned",""
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.Expression.Interactions.dll",""
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.Common.dll","Valid","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.DataVisualization.dll","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.ProcessingObjectModel.dll","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.WinForms.dll","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.SqlServer.Types.dll","Microsoft Corporation"

Voilà!非常感谢@“ Ansgar Wiechers”和@Theo

本文链接:https://www.f2er.com/3145951.html

大家都在问