如何使用PowerShell獲取網站SSL證書的有效期?
SSL證書對於網站至關重要。它們透過啟用HTTPS安全連線,在客戶端和伺服器端之間安全地交換資訊方面發揮著關鍵作用。在下面的文章中,我們將使用PowerShell獲取證書的有效期(起始日期和過期日期)。
為此,我們需要發出**httpwebrequest**請求,但在此之前,我們將使用以下命令忽略SSL警告。
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
然後,我們將透過呼叫.Net類來發出HTTP Web請求。
$url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url)
當我們檢查**$req**時,會顯示一些屬性,但由於我們只對證書日期感興趣,因此我們將使用特定的屬性**ServicePoint**來檢索相關資訊。
$req.ServicePoint
以上命令的輸出。
PS C:\WINDOWS\system32> $req.ServicePoint BindIPEndPointDelegate : ConnectionLeaseTimeout : -1 Address : https://www.microsoft.com/ MaxIdleTime : 100000 UseNagleAlgorithm : True ReceiveBufferSize : -1 Expect100Continue : True IdleSince : 23-06-2020 07:02:36 ProtocolVersion : 1.1 ConnectionName : https ConnectionLimit : 2 CurrentConnections : 0 Certificate : ClientCertificate : SupportsPipelining : True
正如您在上面的屬性中看到的,certificate欄位為空,因此要檢索資訊,我們需要使用**GetResponse()**方法。
$req.GetResponse()
以上命令的輸出。
IsMutuallyAuthenticated : False Cookies : {} Headers : {Pragma, X-Activity-Id, MS-CV, X-AppVersion...} SupportsHeaders : True ContentLength : -1 ContentEncoding : ContentType : text/html; charset=utf-8 CharacterSet : utf-8 Server : LastModified : 23-06-2020 07:06:44 StatusCode : OK StatusDescription : OK ProtocolVersion : 1.1 ResponseUri : https://www.microsoft.com/en-in/ Method : GET IsFromCache : False
現在,我們將執行之前的命令並檢查是否可以檢索證書資訊。
PS C:\WINDOWS\system32> $req.ServicePoint BindIPEndPointDelegate : ConnectionLeaseTimeout : -1 Address : https://www.microsoft.com/en-in/ MaxIdleTime : 100000 UseNagleAlgorithm : True ReceiveBufferSize : -1 Expect100Continue : True IdleSince : 23-06-2020 07:06:44 ProtocolVersion : 1.1 ConnectionName : https ConnectionLimit : 2 CurrentConnections : 1 Certificate : System.Security.Cryptography.X509Certificates.X509Cer tificate ClientCertificate : SupportsPipelining : True
是的,我們可以檢索證書資訊。如果**GetResponse()**命令丟擲異常,您可以使用**try/catch**塊,我將在最終指令碼中使用它。但目前,我們只對檢索證書日期感興趣。
$req.ServicePoint.Certificate
您將看到如下所示的輸出。
在上面的輸出中,日期仍然缺失,因此我們將檢查是否存在任何屬性或方法來檢索日期。我們將檢查日期可用的屬性和方法。
$req.ServicePoint.Certificate | gm | where{$_.Name -like "*Date*"} TypeName: System.Security.Cryptography.X509Certificates.X509Certificate Name MemberType Definition ---- ---------- ---------- GetEffectiveDateString Method string GetEffectiveDateString() GetExpirationDateString Method string GetExpirationDateString()
這裡我們有兩種方法可以獲取過期日期和有效起始日期。
起始日期:
PS C:\WINDOWS\system32> $req.ServicePoint.Certificate.GetEffectiveDateString() 24-06-2019 06:25:35
結束日期:
PS C:\WINDOWS\system32> $req.ServicePoint.Certificate.GetExpirationDateString() 22-10-2021 03:34:04
完整的指令碼如下所示。
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } $url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url) $req.GetResponse() | Out-Null $output = [PSCustomObject]@{ URL = $url 'Cert Start Date' = $req.ServicePoint.Certificate.GetEffectiveDateString() 'Cert End Date' = $req.ServicePoint.Certificate.GetExpirationDateString() } $output URL Cert Start Date Cert End Date --- --------------- ------------- https://www.microsoft.com/ 26-06-2019 09:10:38 22-10-2021 03:34:04
廣告