HTTPS Checker by PowerShell

文章目錄

注意

以下程式是用 ChatGPT 產生的

需求

寫一個簡單的 PowerShell script 來檢查 https 狀況, 包含憑證過期資訊.

PowerShell 7

PowerShell 7 沒有找到合適的方法顯示 SSL 憑證資訊, 目前只顯示 http return code.

 1$urls = @(
 2    "https://example1.com",
 3    "https://example2.com",
 4    "https://example3.com"
 5)
 6
 7foreach ($url in $urls) {
 8    try {
 9        $response = Invoke-WebRequest $url -UseBasicParsing
10        $statusCode = $response.StatusCode
11        Write-Host "$url - Status Code: $statusCode"
12    } catch {
13        Write-Host "$url - Error: $($_.Exception.Message)"
14    }
15}

Output

1PS D:\> .\webmonitor.ps1
2https://example1.com- Status Code: 200
3https://example2.com- Status Code: 200
4https://example3.com- Status Code: 200

PowerShell 5

 1function Get-WebData($url) {
 2    try {
 3        [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
 4
 5        $webRequest = [Net.HttpWebRequest]::Create($url)
 6        $webRequest.ServicePoint.ConnectionLimit = 10
 7        $webRequest.ServicePoint.Expect100Continue = $false
 8        $webRequest.AllowAutoRedirect = $true
 9
10        # Retrieve the response
11        $response = $webRequest.GetResponse()
12        $httpStatusCode = [int]$response.StatusCode
13
14        $certificate = $webRequest.ServicePoint.Certificate
15        if ($certificate -ne $null) {
16            $expiryDate = $certificate.GetExpirationDateString()
17            $expiryDate = Get-Date $expiryDate -Format "yyyy-MM-dd HH:mm:ss"
18            $remainingDays = (Get-Date $expiryDate) - (Get-Date)
19            $response.Close()
20            return @{
21                'Url' = $url
22                'HttpStatusCode' = $httpStatusCode
23                'ExpiryDate' = $expiryDate
24                'RemainingDays' = $remainingDays.Days
25            }
26        } else {
27            $response.Close()
28            return @{
29                'Url' = $url
30                'HttpStatusCode' = $httpStatusCode
31                'ExpiryDate' = "No SSL Certificate found."
32                'RemainingDays' = -1
33            }
34        }
35    } catch {
36        $errorMessage = $_.Exception.Message
37        $innerExceptionMessage = $_.Exception.InnerException.Message
38
39        Write-Host "Error: $errorMessage"
40        Write-Host "Inner Exception: $innerExceptionMessage"
41
42        return @{
43            'Url' = $url
44            'HttpStatusCode' = 0
45            'ExpiryDate' = "Error: $errorMessage"
46            'RemainingDays' = -1
47        }
48    }
49}
50
51$urls = @(
52    "https://example1.com",
53    "https://example22.com",
54    "https://example333.com"
55)
56
57# Find the maximum length of URLs
58$maxUrlLength = ($urls | Measure-Object -Maximum Length).Maximum
59
60foreach ($url in $urls) {
61    $data = Get-WebData $url
62    Write-Host ("{0,-$maxUrlLength} - HTTP Status Code: {1}, Expiry Date: {2} (Remaining Days: {3})" -f $data.Url, $data.HttpStatusCode, $data.ExpiryDate, $data.RemainingDays)
63}

Output

1PS D:\> .\webmonitor.ps1
2https://example1.com   - HTTP Status Code: 200, Expiry Date: 2023-11-22 07:59:59 (Remaining Days: 110)
3https://example22.com  - HTTP Status Code: 200, Expiry Date: 2023-11-22 07:59:59 (Remaining Days: 110)
4https://example333.com - HTTP Status Code: 200, Expiry Date: 2024-06-25 07:59:59 (Remaining Days: 326)