Powershell 根据字段条件筛选PSCustomObject值
您好,我有以下脚本,它允许我获取证书详细信息并将它们存储在Powershell 根据字段条件筛选PSCustomObject值,powershell,csv,ssl,Powershell,Csv,Ssl,您好,我有以下脚本,它允许我获取证书详细信息并将它们存储在CSV文件中。我想排除出现在CSV文件中的一些证书信息,这些证书在到期前还有3650天 这是我的剧本 $StartDate = Get-Date $CertPath = 'Cert:\LocalMachine\' $CertsDetail = Get-ChildItem -Path $CertPath -Recurse | Where-Object { $_.PsIsContainer -ne $true } | ForEach-Obj
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 ...
您的对象中已经有一个字段,在剩余的天数内,因此在
$CertsDetail
集合中使用Where object
可以很简单地筛选出您不想要的结果:
$CertsDetail | Where-Object { $_.DaysRemaining -lt 3650 } | Export-CSV ...
你是说365天吗?不,我是说超过10年你是说365天吗?不,我是说超过10年