gpt4 book ai didi

powershell - 格式化发行者输出

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:50 25 4
gpt4 key购买 nike

在下面的代码中,如您所见,我愿意收集有关 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com