Last active
February 14, 2022 09:55
-
-
Save JesperJ/038b53b2e33903c82f2f93cdbca6dfcb to your computer and use it in GitHub Desktop.
Check for expiring SSL certificates
This file contains 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
# Written for Powershell 5.1 | |
function Get-SSLCertificateDates { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[String] | |
$URL | |
) | |
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } | |
$req = [Net.HttpWebRequest]::Create($URL) | |
try { | |
$req.GetResponse() | Out-Null | |
} | |
catch {} | |
$cert = [PSCustomObject]@{ | |
URL = $URL | |
'CertStartDate' = $req.ServicePoint.Certificate.GetEffectiveDateString() | |
'CertEndDate' = $req.ServicePoint.Certificate.GetExpirationDateString() | |
} | |
$cert | |
} | |
$urls = @() | |
$urls += "https://www.microsoft.com" | |
$urls += "https://www.google.com" | |
$urls += "https://expired.badssl.com" | |
foreach ($url in $urls) { | |
$SSLCertificateDates = Get-SSLCertificateDates -URL $url | |
$today = Get-Date | |
$certWarnDate = (Get-Date $($SSLCertificateDates.CertEndDate)).AddDays(-90) | |
if ($today -gt $certWarnDate) { | |
"Certificate warning for $url, expiring on $($SSLCertificateDates.CertEndDate)." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment