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

[玩转系统] 在 PowerShell 中检查 SSL 证书到期日期

作者:精品下载站 日期:2024-12-14 22:00:47 浏览:13 分类:玩电脑

在 PowerShell 中检查 SSL 证书到期日期


SSL(安全套接字层)是一种数字证书,可在服务器和客户端之间提供加密连接并验证网站身份。为了保证用户敏感数据的安全并为用户提供信任,检查 SSL 证书到期情况并在到期时续订非常重要。

在 PowerShell 中,使用 [Net.HttpWebRequest] 向网站发出 HTTP Web 请求并获取与其关联的所有属性以及证书详细信息。它将有助于查找 SSL 证书的到期日期。

在本文中,我们将讨论如何在 PowerShell 中获取 SSL 证书到期日期。

PowerShell 获取 SSL 证书到期日期

.Net库提供了[System.Net.ServicePoint]库来管理ServicePoint对象的集合。 ServicePointManager 返回包含有关 Internet 资源 URI 信息的 ServicePoint 对象。

使用 ServicePointManager.ServerCertificateValidationCallBack 属性获取或设置验证服务器证书的回调。

使用 [Net.HttpWebRequest] 库获取有关网站的 SSL 证书详细信息,并使用 PowerShell 脚本提取 SSL 证书到期日期。

运行以下 Windows PowerShell 脚本来检查 SSL 证书到期日期。

# Ignore SSL Warning
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# Create Web Http request to URI
$uri = "https://google.com/" 
$webRequest = [Net.HttpWebRequest]::Create($uri)

# Get URL Information
$webRequest.ServicePoint

# Retrieve the Information for URI
$webRequest.GetResponse() | Out-NULL

# Get SSL Certificate and its details
$webRequest.ServicePoint.Certificate

# Get SSL Certificate Expiration Date
$webRequest.ServicePoint.Certificate.GetExpirationDateString()

在上面的 PowerShell 脚本中,它使用 [Net.HttpWebRequest] 创建对网站 URI 的 HTTP Web 请求,并在 $webRequest 变量。

$webRequest.GetResponse() 检索 URI 的响应并获取 SSL 证书详细信息、服务器、标头和其他相关信息。

$webRequest.ServicePoint.Certificate 获取证书详细信息,例如颁发者、句柄和 SSL 证书指纹。使用 GetExpirationDateString() 方法通过 PowerShell 检查网站的 SSL 过期日期。

上述用于查找网站 SSL 证书到期日期的 PowerShell 脚本的输出为:

[玩转系统] 在 PowerShell 中检查 SSL 证书到期日期

PS D:\> $webRequest.GetResponse()


IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {X-XSS-Protection, X-Frame-Options, Cache-Control, Content-Type...}
SupportsHeaders         : True
ContentLength           : -1
ContentEncoding         :
ContentType             : text/html; charset=ISO-8859-1
CharacterSet            : ISO-8859-1
Server                  : gws
LastModified            : 31-01-2023 10:52:14
StatusCode              : OK
StatusDescription       : OK
ProtocolVersion         : 1.1
ResponseUri             : https://www.google.com/
Method                  : GET
IsFromCache             : False



PS D:\> $webRequest.ServicePoint.Certificate.GetExpirationDateString()
03-04-2023 13:49:11
PS D:\>

酷提示:如何在 PowerShell 中获取证书详细信息!

结论

希望以上关于如何在PowerShell中检查SSL证书过期日期的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯