当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 检查 SSL/TLS 证书到期日期

作者:精品下载站 日期:2024-12-14 22:54:43 浏览:15 分类:玩电脑

使用 PowerShell 检查 SSL/TLS 证书到期日期


服务器证书的意外过期可能会给您的用户和客户带来许多问题:他们可能无法与您的站点建立安全连接,可能会发生身份验证错误,浏览器中可能会出现烦人的通知等。在本文中,我们将展示如何检查远程站点上的 SSL/TLS 证书的过期日期,或获取域中服务器或计算机上的本地证书存储中的过期证书列表。

使用 PowerShell 获取网站 SSL 证书的到期日期

许多 Web 项目使用免费的 Let’s Encrypt SSL 证书来实现 HTTPS。这些证书的有效期为 90 天,必须定期更新。通常,特殊脚本或机器人会在托管或服务器端更新 Let’s Encrypt 证书(可能是 Windows 中的 WACS 或 Linux 中的 Certbot)。但是,有时自动证书续订会失败。我想要自己的脚本来检查网站上的 SSL 证书到期日期,并在它们即将到期时通知我。我使用 PowerShell 来创建它。由于我们通过 HttpWeb 查询检查网站的证书,因此不需要远程网站/服务器的管理员权限。

在以下 PowerShell 脚本中,您必须指定要检查证书到期日期的网站列表以及开始向您显示相应通知时的证书期限 (

$minCertAge

)。我输入了 80 天作为示例。

$minCertAge = 80
$timeoutMs = 10000
$sites = @(
"https://testsite1.com/",
"https://testsite2.com/",
"https://a-d.site/"
)
# Disable certificate validation
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
foreach ($site in $sites)
{
Write-Host Check $site -f Green
$req = [Net.HttpWebRequest]::Create($site)
$req.Timeout = $timeoutMs
try {$req.GetResponse() |Out-Null} catch {Write-Host URL check error $site`: $_ -f Red}
$expDate = $req.ServicePoint.Certificate.GetExpirationDateString()
$certExpDate = [datetime]::ParseExact($expDate, “dd/MM/yyyy HH:mm:ss”, $null)
[int]$certExpiresIn = ($certExpDate - $(get-date)).Days
$certName = $req.ServicePoint.Certificate.GetName()
$certThumbprint = $req.ServicePoint.Certificate.GetCertHashString()
$certEffectiveDate = $req.ServicePoint.Certificate.GetEffectiveDateString()
$certIssuer = $req.ServicePoint.Certificate.GetIssuerName()
if ($certExpiresIn -gt $minCertAge)
{Write-Host The $site certificate expires in $certExpiresIn days [$certExpDate] -f Green}
else
{
$message= "The $site certificate expires in $certExpiresIn days"
$messagetitle= "Renew certificate"
Write-Host $message [$certExpDate]. Details:`n`nCert name: $certName`Cert thumbprint: $certThumbprint`nCert effective date: $certEffectiveDate`nCert issuer: $certIssuer -f Red
#Displays a pop-up notification and sends an email to the administrator
#ShowNotification $messagetitle $message
# Send-MailMessage -From [email protected] -To [email protected] -Subject $messagetitle -body $message -SmtpServer gwsmtp.a-d.site -Encoding UTF8
}
write-host "________________" `n
}

此 PowerShell 脚本将检查列表中所有网站的 SSL 证书。如果发现证书即将过期,则会在通知中突出显示。

[玩转系统] 使用 PowerShell 检查 SSL/TLS 证书到期日期

要通知管理员 SSL 证书即将过期,您可以添加弹出通知。为此,请取消注释脚本行“

ShowNotification $messagetitle $message

”并添加以下函数:

Function ShowNotification ($MsgTitle, $MsgText) {
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = $MsgText
$balmsg.BalloonTipTitle = $MsgTitle
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(10000)
}

您还可以使用发送电子邮件通知

Send-MailMessage

然后,如果发现任何已过期或即将到期的证书,您将收到一封电子邮件和一条弹出消息通知。

[玩转系统] 使用 PowerShell 检查 SSL/TLS 证书到期日期

然后为任务计划程序创建一个每周运行一次或两次的自动任务,并运行 PowerShell 脚本来检查 HTTPS 网站证书的到期日期。 (您可以使用 Register-ScheduledTask cmdlet 在任务计划程序中创建任务来运行 PS1 脚本文件。)

如何远程检查Windows证书存储中过期的证书?

您可能还需要 PowerShell 脚本来检查域服务器(例如 RDP/RDS、Exchange、SharePoint、LDAPS 证书等)或用户计算机上的加密服务所使用的证书的到期日期。

在本地计算机上,您可以使用以下命令获取证书列表:

Get-ChildItem -Path cert

Powershell 3.0有一个特殊的

-ExpiringInDays

争论:

Get-ChildItem -Path cert: -Recurse -ExpiringInDays 30

在 PowerShell 2.0 中,相同的命令如下所示:

Get-ChildItem -Path cert: -Recurse | where { $_.notafter -le (get-date).AddDays(30) -AND $_.notafter -gt (get-date)} | select thumbprint, subject

要仅检查您自己的证书,请使用

Cert:\LocalMachine\My

容器而不是

Cert:

在根文件夹中。因此,您不会检查 Windows 受信任的根证书和商业证书。

要查找所有域服务器上将在未来 30 天内过期的证书,请使用以下 PowerShell 脚本:

$servers= (Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(operatingSystem=Windows Server*) (!serviceprincipalname=*MSClusterVirtualServer*) (!(userAccountControl:1.2.840.113556.1.4.803:=2)))").Name
$result=@()
foreach ($server in $servers)
{
$ErrorActionPreference="SilentlyContinue"
$getcert=Invoke-Command -ComputerName $server { Get-ChildItem -Path Cert:\LocalMachine\My -Recurse -ExpiringInDays 30}
foreach ($cert in $getcert) {
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Server'=$server;
'Certificate'=$cert.Issuer;
'Expires'=$cert.NotAfter
})
}
}
Write-Output $result

[玩转系统] 使用 PowerShell 检查 SSL/TLS 证书到期日期

您将获得即将过期的服务器证书列表,并且您将有足够的时间来更新它们。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯