Как выбрать пару имя / значение в Select-Object? - PullRequest
0 голосов
/ 07 ноября 2019

Для проверки цифровой подписи сборок я использую 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, OU=MOPR, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

Как можноЯ уменьшаю вывод до единственного значения: «Microsoft Corporation»? Выходной файл, который я хотел бы получить, имеет вид 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,NotSigned,
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,Valid,Microsoft Corporation

Ответы [ 3 ]

2 голосов
/ 07 ноября 2019

Я бы сделал это, используя небольшую вспомогательную функцию для анализа имени эмитента из 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)}}
1 голос
/ 07 ноября 2019

Удалите ненужные части предмета в вычисленном свойстве:

@{n='SubjectName', e={$_.SignerCertificate.Subject -replace ',.*' -replace '^CN='}}
0 голосов
/ 07 ноября 2019

Для суммирования:

  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] }
    elseif ($IssuerDN -match 'CN=([^,]+)')  { $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, Status, 
        @{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","NotSigned",""
"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","Valid","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.ProcessingObjectModel.dll","Valid","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.ReportViewer.WinForms.dll","Valid","Microsoft Corporation"
"C:\Program Files (x86)\My Company\My Product\Components\Microsoft.SqlServer.Types.dll","Valid","Microsoft Corporation"

Вуаля! Большое спасибо @ Ansgar Wiechers и @ Theo

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...