Powershell 在证书即将过期时发出通知
Powershell notify when certificate almost expires
在 Powershell 中,我想在域控制器中的证书提前 24 小时过期时通知特定用户。我已经找到一个代码,然后显示开始日期和到期日期以及剩余天数。但是当证书过期时我如何得到通知(通过电子邮件)。
Get-ChildItem Cert:\LocalMachine\My `
| Select @{N='StartDate';E={$_.NotBefore}},
@{N='EndDate';E={$_.NotAfter}},
@{N='DaysRemaining';E={($_.NotAfter - (Get-Date)).Days}}
我建议向您 select 的对象添加更多属性,以便以后更好地识别,并向其添加 Where-Object{}
子句,以过滤之前剩余 1 天或更短时间的证书即将到期:
$certs = Get-ChildItem -Path 'Cert:\LocalMachine\My' |
Select-Object Thumbprint, Issuer,
@{N='StartDate';E={$_.NotBefore}},
@{N='EndDate';E={$_.NotAfter}},
@{N='DaysRemaining';E={($_.NotAfter - (Get-Date).Date).Days}} |
Where-Object { $_.DaysRemaining -le 1 }
if ($certs) {
# send an email to yourself
$mailSplat = @{
To = 'me@mycompany.com'
From = 'me@mycompany.com'
SmtpServer = 'MySmtpServer'
Subject = 'Expiring certificates'
Body = $certs | Format-List | Out-String
# other params can go here
}
# send the email
Send-MailMessage @mailSplat
}
(Get-Date).Date
取今天午夜的日期,而不是你 运行 脚本
的当前时间
在 Powershell 中,我想在域控制器中的证书提前 24 小时过期时通知特定用户。我已经找到一个代码,然后显示开始日期和到期日期以及剩余天数。但是当证书过期时我如何得到通知(通过电子邮件)。
Get-ChildItem Cert:\LocalMachine\My `
| Select @{N='StartDate';E={$_.NotBefore}},
@{N='EndDate';E={$_.NotAfter}},
@{N='DaysRemaining';E={($_.NotAfter - (Get-Date)).Days}}
我建议向您 select 的对象添加更多属性,以便以后更好地识别,并向其添加 Where-Object{}
子句,以过滤之前剩余 1 天或更短时间的证书即将到期:
$certs = Get-ChildItem -Path 'Cert:\LocalMachine\My' |
Select-Object Thumbprint, Issuer,
@{N='StartDate';E={$_.NotBefore}},
@{N='EndDate';E={$_.NotAfter}},
@{N='DaysRemaining';E={($_.NotAfter - (Get-Date).Date).Days}} |
Where-Object { $_.DaysRemaining -le 1 }
if ($certs) {
# send an email to yourself
$mailSplat = @{
To = 'me@mycompany.com'
From = 'me@mycompany.com'
SmtpServer = 'MySmtpServer'
Subject = 'Expiring certificates'
Body = $certs | Format-List | Out-String
# other params can go here
}
# send the email
Send-MailMessage @mailSplat
}
(Get-Date).Date
取今天午夜的日期,而不是你 运行 脚本