在结果中排​​除负整数

时间:2018-09-27 18:14:55

标签: object certificate powershell-v4.0

我正在尝试检查过期的证书。问题是我们有时将过期的留在服务器上以供参考,而我只想显示导致过期的那些。在这种情况下,如何排除负整数?

示例输出:

有效期(天): -25 不晚于:9/1/2018 10:56:27 AM主题:CN = SERVER01.domain.com发行方:CN = SOMEAUTHICA01,DC = domain, DC = com

有效期(天): -17 不晚于:9/9/2018 3:45:55 PM主题:CN = SERVER02.domain.com发行方:CN = SOMEAUTHICA01,DC = domain, DC = com

$threshold = 30   #Number of days to look for expiring certificates
$deadline = (Get-Date).AddDays($threshold)   #Set expiration deadline date
$serverList = 'SERVER01.domain.com','SERVER02.domain.com'

$expired = Invoke-Command -ComputerName $serverList { Dir Cert:\LocalMachine\My } | Sort-Object -Property NotAfter | Get-Unique | foreach {
    If ($_.NotAfter -le $deadline) {$_ | Select Issuer, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} } 
 }

1 个答案:

答案 0 :(得分:0)

您必须排除已经过期的证书。可以按照以下步骤进行。

$threshold = 30;   #Number of days to look for expiring certificates
$deadline = (Get-Date).AddDays($threshold);   #Set expiration deadline date
$serverList = 'SERVER01.domain.com','SERVER02.domain.com';

$expired = Invoke-Command -ComputerName $serverList { Dir Cert:\LocalMachine\My } | Sort-Object -Property NotAfter | Get-Unique | foreach {
    If ($_.NotAfter -le $deadline -and $_.NotAfter -gt (Get-Date).Days) {$_ | Select Issuer, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} } 
}