格式发行者输出

时间:2019-05-23 09:01:19

标签: powershell ssl

在下面的代码中,您将看到我愿意收集有关SSL证书的信息,但是字段IssuerUsages的格式存在两个问题。 第一个字段Issuer的示例向我显示了以下结果:

  

O = VMware Installer

     

CN = 2014年Microsoft开发根证书颁发机构,O = Microsoft公司,L = Redmond,S =华盛顿,C =美国

我想要的是结果

  

VMware Installer

     

2014年Microsoft开发根证书颁发机构

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

  

数据加密,密钥加密,数字签名

     

值FriendlyName

     

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

     

1.3.6.1.5.5.7.3.1服务器身份验证

我想显示的是:

  

数据加密,密钥加密,数字签名,服务器身份验证

这是我的脚本:

  $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'

1 个答案:

答案 0 :(得分:0)

也许像下面这样会为您提供所需的格式:

$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