gpt4 book ai didi

powershell - 忽略自签名证书

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

我有这段代码列出了所有本地计算机 SSL 证书的详细信息,并将它们存储在 csv 文件中。然而,有一些自签名证书我发现它们没有用,所以我想将它们排除在外,但我做错了

下面是我写的代码

$StartDate = Get-Date
$CertPath = 'Cert:\LocalMachine\'
$CertsDetail = Get-ChildItem -Path $CertPath -Recurse | Where-Object {
$_.PsIsContainer -ne $true} | ForEach-Object {
$DaysLeft = (New-TimeSpan -Start $StartDate -End $_.NotAfter).Days
if ($DaysLeft -lt 1) {
$Under30 = $true
$Expired = $true
$Text = "The Certificate is expired"
} elseif ($DaysLeft -lt 30) {
$Under30 = $true
$Expired = $false
$Text = "The Certificate is but valid about to expire"
} else {
$Under30 = $false
$Expired = $false
$Text = "The Certificate is still valid and not going soon to expire"
}
$FinalDate = Get-Date $_.NotAfter -Format 'dd/MM/yyyy hh:mm'
$Usages = ($_.Extensions | Where-Object {$_.KeyUsages}).KeyUsages
if ($Usages) {

$issuer = '{0}, {1}' -f
([regex] 'O=([^,]+)').Match($_.Issuer).Groups[1].Value,
([regex] 'CN=([^,]+)').Match($_.Issuer).Groups[1].Value
$issuer = $issuer.Trim(", ")
[PSCustomObject]@{
Text = $Text
Issuer = $issuer.TrimStart('"')

Subject = $_.Subject
ExpireDate = $FinalDate
DaysRemaining = $DaysLeft
Usages = $Usages.ToString() -replace ',', ';'
Under30Days = $Under30
Expired = $Expired

}
}
}

$CertsDetail | Where-Object {$_.DaysRemaining -lt 3650 -and $_.Usages -ne ""
} | Export-Csv -NoTypeInformation -Path 'C:\SECnology\Data\Utilities\Certificate_State.csv'

最佳答案

对于自签名证书,SubjectIssuer 字段将相同:

# Filter out self-signed certificates
Get-ChildItem -Path $CertPath -Recurse |Where { $_.Subject -ne $_.Issuer }

关于powershell - 忽略自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339245/

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