【发布时间】:2019-05-28 09:39:20
【问题描述】:
我有这段代码列出了所有本地计算机 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'
【问题讨论】:
-
你为什么发布带有破坏它的格式的代码?适当的缩进也有助于阅读代码。
-
有些行有两行写不完
标签: powershell ssl