您好,我有以下脚本可以让我获取证书详细信息并将它们存储在 CSV
文件中。我想排除一些证书信息出现在 CSV
中文件是距离到期还有超过 3650 天的证书
这是我的脚本
$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'
[PSCustomObject]@{
Text = $Text
Subject = $_.Subject
ExpireDate = $FinalDate
DaysRemaining = $DaysLeft
Under30Days = $Under30
Expired = $Expired
}
}
$CertsDetail | Export-Csv -NoTypeInformation -Path'C:\SECnology\Data\Utilities\Certificate_State.csv'
最佳答案
您的对象中已经有一个剩余天数的字段,因此这是一个简单的例子,即在您的 $CertsDetail
集合上使用 Where-Object
来过滤掉您想要的结果不想:
$CertsDetail | Where-Object { $_.DaysRemaining -lt 3650 } | Export-CSV ...
关于powershell - 根据字段条件过滤 PSCustomObject 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219737/