[玩转系统] 使用 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 证书。如果发现证书即将过期,则会在通知中突出显示。
要通知管理员 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 脚本来检查 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
您将获得即将过期的服务器证书列表,并且您将有足够的时间来更新它们。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag