根据字段条件过滤PSCustomObject值

时间:2019-05-20 11:08:50

标签: powershell csv ssl

您好,我具有以下脚本,该脚本可以让我获取证书详细信息并将其存储在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'

1 个答案:

答案 0 :(得分:0)

您的对象中已经有剩余天数的字段,因此这是在Where-Object集合中使用$CertsDetail来过滤掉不需要的结果的简单情况:

$CertsDetail | Where-Object { $_.DaysRemaining -lt 3650 } | Export-CSV ...