powershell - 格式化发行者输出

标签 powershell ssl

在下面的代码中,如您所见,我愿意收集有关 SSL 证书的信息,但我对字段 IssuerUsages 的格式有两个问题跟上。 第一个字段 Issuer fir 示例显示如下结果:

O=VMware Installer

CN=Microsoft Development Root Certificate Authority 2014, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

我想要的是结果

VMware Installer

Microsoft Development Root Certificate Authority 2014

对于第二个字段Usages,我有例如以下输出

DataEncipherment, KeyEncipherment, DigitalSignature

Value FriendlyName

----- ------------

1.3.6.1.5.5.7.3.1 Server Authentication

我想展示的是:

DataEncipherment, KeyEncipherment, DigitalSignature,Server Authentication

这是我的脚本:

  $CertPath = "Cert:\LocalMachine\"
  $CertsDetail = Get-ChildItem -Path $CertPath -Recurse |`
        Where-Object { $_.PsIsContainer -ne $true } |`
        ForEach-Object {
              $Usages = foreach ($key in $_.Extensions) {
                    if ($key.KeyUsages) {
                          $key.KeyUsages
                    }
              }         
              [PSCustomObject]@{
                    Issuer  = $_.Issuer
                    Subject = $_.Subject
                    Usages  = ($Usages | Out-String).Trim()
                    #Usages = $Usages -join ';'
              }
        }
  $CertsDetail
  $CertsDetail | Where-Object { $_.Usages -ne "" } | Export-Csv -NoTypeInformation -Path 'C:\SECnology\Data\Utilities\Certificate_State.csv'

最佳答案

也许像下面那样做会给你你想要的格式:

$CertPath = 'Cert:\LocalMachine\'             #'# dummy comment to correct code-highlighting in SO
$CertsDetail = Get-ChildItem -Path $CertPath -Recurse | Where-Object { !$_.PsIsContainer } | ForEach-Object {
    $Usages = ($_.Extensions | Where-Object {$_.KeyUsages}).KeyUsages
    if ($Usages) {
        # get at most two parts out of the $_.Issuer string
        $issuer = '{0}, {1}' -f ([regex] 'O=([^,]+)').Match($_.Issuer).Groups[1].Value, 
                                ([regex] 'CN=([^,]+)').Match($_.Issuer).Groups[1].Value
        [PSCustomObject]@{
            Issuer  = $issuer.Trim(", ")
            Subject = $_.Subject
            Usages  = $Usages.ToString() -replace ',', ';'
        }
    }
}

$CertsDetail | Format-List
$CertsDetail | Export-Csv -NoTypeInformation -Path 'C:\SECnology\Data\Utilities\Certificate_State.csv'

在我的(Windows 7)机器上尝试这个,它给了我这样的输出

Issuer  : Microsoft Root Authority
Subject : CN=Microsoft Enforced Licensing Intermediate PCA, OU=Copyright (c) 1999 Microsoft Corp., O=Microsoft Corporation, L=Redmond, S=Washington, C=US
Usages  : CrlSign; KeyCertSign; DigitalSignature

Issuer  : The USERTRUST Network, UTN-USERFirst-Hardware
Subject : CN=www.google.com, OU=PlatinumSSL, OU=Hosted by GTI Group Corporation, OU=Tech Dept., O=Google Ltd., STREET=Sea Village 10, L=English, S=Florida, PostalCode=38477, C=US
Usages  : KeyEncipherment; DigitalSignature

关于powershell - 格式化发行者输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271605/

相关文章:

powershell - 如何定义函数的返回类型/OutputType

azure - 通过 PowerShell 从本地 SQL Server 2016 将 dacpac 发布到 Azure SQL 托管实例失败 "cannot publish to SQL Server 2014"

PowerShell:复制项目找不到路径

ssl - 如果我想限制对特定服务器组的 SSL/DTLS 访问,只比较 Base64/pem 文件有什么问题?

apache - 无法使用 mamp 建立连接

python - 在 splinter 中强制使用 SSLv3 或 TLSV1

swift - 如何验证 Assistant SDK 的 gRPC 调用?

powershell - PowerShell Rename-Item正在创建文件的副本

apache - 在服务器 b.example.com 上配置 Apache 以允许在没有警告的情况下从 example.com 请求 b.example.com 上的安全资源?

file - 如何使用 PowerShell 删除空子文件夹?