Skip to content

Instantly share code, notes, and snippets.

@JesperJ
Last active February 14, 2022 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesperJ/038b53b2e33903c82f2f93cdbca6dfcb to your computer and use it in GitHub Desktop.
Save JesperJ/038b53b2e33903c82f2f93cdbca6dfcb to your computer and use it in GitHub Desktop.
Check for expiring SSL certificates
# 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