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

[玩转系统] PowerShell 获取 SSL 证书信息

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

PowerShell 获取 SSL 证书信息


使用 [Net.HttpWebRequest] 库创建与网站 URI 和 GetResponse() 的连接,其中包含 SSL 证书信息,如句柄、颁发者、主题、到期日期等...

在本文中,我们将讨论如何使用 PowerShell 从网站检索 SSL 证书详细信息。

PowerShell 从 URL 获取 SSL 证书

使用具有 Create() 方法的 [Net.HttpWebRequest] 库。它接受网站 URL 作为输入参数,并创建到该网站的 Web 请求连接。

使用 GetResponse() 方法从 URL 获取 SSL 证书详细信息。

[Net.ServicePointManager]::ServerCertificateValidationCallback={ $true } 用于在向 URI 发出 Web 请求时忽略 SSL 证书错误或警告。

运行以下 PowerShell 脚本以从网站 URL 检索 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 information
$webRequest.ServicePoint.Certificate

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

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

$webRequest.ServicePoint.Certificate 获取证书详细信息,例如颁发者、句柄和 SSL 证书指纹。

上述用于从 URL 获取 SSL 证书详细信息的 PowerShell 脚本的输出为:

[玩转系统] PowerShell 获取 SSL 证书信息

PowerShell 获取 SSL 证书颁发者名称

SSL 证书具有 Issuer 属性或 GetIssuerName() 方法来检索 SSL 证书颁发者名称。

运行以下PowerShell脚本获取SSL证书颁发者名称,参考上述PowerShell脚本向网站发出Web请求并获取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 Issuer Name
$webRequest.ServicePoint.Certificate | Select Issuer

# Use GetIssuerName() to obtain SSL certificate issuer name
$webRequest.ServicePoint.Certificate.GetIssuerName()

上述 PowerShell 脚本的输出检索 SSL 证书颁发者名称,如下所示:

PS D:\> $webRequest.ServicePoint.Certificate | Select Issuer                                                                   
Issuer
------
CN=GTS CA 1C3, O=Google Trust Services LLC, C=US

PS D:\> $webRequest.ServicePoint.Certificate.GetIssuerName()                                                                   C=US, O=Google Trust Services LLC, CN=GTS CA 1C3

查找 SSL 证书主题名称

SSL 证书有一个Subject 属性和GetName() 方法,用于获取SSL 证书主题名称。

请参阅上面的 PowerShell 脚本,该脚本向网站发出 Web 请求并获取 SSL 证书信息。

# Get SSL Certificate Subject Name
$webRequest.ServicePoint.Certificate | Select Subject

# Use GetName() to obtain SSL certificate Subject name
$webRequest.ServicePoint.Certificate.GetName()

上面的 PowerShell 脚本使用 Subject 属性来检索 SSL 证书的使用者名称。另一种方法是使用 GetName() 方法获取 SSL 证书使用者名称。

上述脚本的输出是:

PS D:\> $webRequest.ServicePoint.Certificate | Select Subject                                                                  
Subject
-------
CN=www.google.com


PS D:\> $webRequest.ServicePoint.Certificate.GetName()                                                                         CN=www.google.com

查找 SSL 证书序列号

使用SSL证书的GetSerialNumberString()方法获取SSL证书序列号。

请参阅上面的 PowerShell 脚本,该脚本向 URL 发出 Web 请求并获取证书详细信息。

# Use GetName() to obtain SSL certificate SerialNumber
$webRequest.ServicePoint.Certificate.GetSerialNumberString()

上述PowerShell脚本使用GetSerialNumberString()方法获取SSL证书序列号。

上述脚本的输出是:

PS D:\> $webRequest.ServicePoint.Certificate.GetSerialNumberString()                                                           229AE03988E214FE0A531413A2A103BE

酷提示:如何在 PowerShell 中检查 SSL 证书到期日期!

结论

希望以上关于如何在PowerShell中获取SSL证书信息的文章对您有所帮助。

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

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

取消回复欢迎 发表评论:

关灯