powershell - 根据字段条件过滤 PSCustomObject 值

标签 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'

最佳答案

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

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

关于powershell - 根据字段条件过滤 PSCustomObject 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219737/

相关文章:

bash - 在 powershell 中等待命令输出中的文本

powershell - 检查处理器体系结构并继续执行if语句

mysql - 比较两个 csv 文件的内容,其中两个文件之间的关系在第三个文件中指定?

.htaccess - AWS Elasticbeanstalk 单实例强制 SSL 重定向循环

php - 如何检索推送通知 php 的 ck.pem 文件

powershell - 嵌套每次展平?

windows - 反引号放置为何重要?

Python-网页抓取 : TypeError: string indices must be integers

python - 平均数从最高到最低

java - org.bouncycaSTLe.tls.crypto.TlsCertificate getSubject() 和其他 getter