Last active
January 7, 2024 22:47
-
-
Save davidlu1001/f6660cbdff894bbc42168a35176fb531 to your computer and use it in GitHub Desktop.
Get SSL Certificate Expiry Date From URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-SSLCertificateExpiryDetailsFromURL { | |
| param ( | |
| [ValidateNotNullOrEmpty()] | |
| [string]$InputFilePath = ".\input.csv", | |
| [string]$OutputFilePath = ".\output.csv", | |
| [bool]$ExportToCsv = $true, # Control CSV export | |
| [int]$TimeoutMilliseconds = 20000, # Timeout duration in milliseconds | |
| [int]$MaxRetries = 3, # Number of retries for each request | |
| [int]$IntervalSeconds = 5 # Interval in seconds between each request | |
| ) | |
| function Get-SSLCertificateExpiryDateFromURL { | |
| param ( | |
| [string]$Url, | |
| [int]$Timeout, | |
| [int]$RetryCount, | |
| [int]$Interval | |
| ) | |
| $attempt = 0 | |
| while ($attempt -lt $RetryCount) { | |
| try { | |
| $uri = if (-not $Url.StartsWith("https://")) { "https://$Url" } else { $Url } | |
| $uri = New-Object System.Uri($uri) | |
| $request = [Net.HttpWebRequest][Net.WebRequest]::Create($uri) | |
| $request.Timeout = $Timeout | |
| $response = $request.GetResponse() | |
| $certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($request.ServicePoint.Certificate) | |
| $expiryDate = $certificate.NotAfter | |
| $formattedExpiryDate = $expiryDate.ToString("dd-MM-yyyy") | |
| $daysLeft = ($expiryDate - [datetime]::Now).Days | |
| return @{ | |
| 'DNSName' = $uri.Host; | |
| 'ExpiryDate' = $formattedExpiryDate; | |
| 'DaysLeft' = $daysLeft | |
| } | |
| } catch { | |
| Write-Host "Attempt $($attempt + 1) failed for $Url : $_" | |
| $attempt++ | |
| Start-Sleep -Seconds $Interval # Pause before retrying | |
| } | |
| } | |
| Write-Host "Error processing $Url after $RetryCount attempts." | |
| return $null | |
| } | |
| if (-not (Test-Path -Path $InputFilePath)) { | |
| Write-Error "Input file not found at path: $InputFilePath" | |
| return | |
| } | |
| $entries = Import-Csv -Path $InputFilePath | |
| if ($entries.Count -eq 0) { | |
| Write-Host "No entries found in the input file." | |
| return | |
| } | |
| $results = foreach ($entry in $entries) { | |
| if ([string]::IsNullOrWhiteSpace($entry.DNSName)) { | |
| Write-Host "Skipping empty DNSName entry." | |
| continue | |
| } | |
| $expiryDetails = Get-SSLCertificateExpiryDateFromURL -Url $entry.DNSName -Timeout $TimeoutMilliseconds -RetryCount $MaxRetries -Interval $IntervalSeconds | |
| if ($expiryDetails -ne $null) { | |
| [PSCustomObject]@{ | |
| DNSName = $expiryDetails.DNSName; | |
| FriendlyName = $entry.FriendlyName; | |
| DaysLeft = $expiryDetails.DaysLeft; | |
| ExpiryDate = $expiryDetails.ExpiryDate | |
| } | |
| } | |
| Start-Sleep -Seconds $IntervalSeconds # Sleep after processing each URL | |
| } | |
| if ($ExportToCsv) { | |
| $results | Export-Csv -Path $OutputFilePath -NoTypeInformation | |
| Write-Host "SSL certificate details exported to $OutputFilePath" | |
| } else { | |
| $results | |
| } | |
| } | |
| Get-SSLCertificateExpiryDetailsFromURL -InputFilePath ".\input.csv" -OutputFilePath ".\output.csv" -ExportToCsv $false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment