Last active
March 5, 2021 10:03
-
-
Save cisterni/d832a4a32ef7939d25185e2ecc97dbb6 to your computer and use it in GitHub Desktop.
TLS Certificate test in powershell core
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
function Test-WebServerCertificate { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] | |
[string]$URL, | |
[Parameter(Position = 1)] | |
[ValidateRange(1,65535)] | |
[int]$Port = 443, | |
[Parameter(Position = 2)] | |
[Net.WebProxy]$Proxy, | |
[Parameter(Position = 3)] | |
[int]$Timeout = 15000, | |
[switch]$UseUserContext | |
) | |
Process { | |
$ConnectString = "https://$url`:$port" | |
$WebRequest = [Net.WebRequest]::Create($ConnectString) | |
$WebRequest.Proxy = $Proxy | |
$WebRequest.Credentials = $null | |
$WebRequest.Timeout = $Timeout | |
$WebRequest.AllowAutoRedirect = $true | |
$cert = [PSCustomObject]@{ | |
URI = $ConnectString | |
Issuer = $null | |
Subject = $null | |
NotBefore = $null | |
NotAfter = $null | |
} | |
$WebRequest.ServerCertificateValidationCallback = { | |
$cert.Issuer = $args[1].Issuer | |
$cert.Subject = $args[1].Subject | |
$cert.NotBefore = $args[1].NotBefore | |
$cert.NotAfter = $args[1].NotAfter | |
$true | |
} | |
try {$Response = $WebRequest.GetResponse()} | |
catch {} | |
$cert | |
} | |
} | |
$sites = @( | |
"www.facebook.com" | |
) | |
$sites | Test-WebServerCertificate | where { $_.NotAfter -lt ([DateTime]::Now.AddMonths(10)) } | select URI, NotAfter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment