Skip to content

Instantly share code, notes, and snippets.

@cisterni
Last active March 5, 2021 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cisterni/d832a4a32ef7939d25185e2ecc97dbb6 to your computer and use it in GitHub Desktop.
Save cisterni/d832a4a32ef7939d25185e2ecc97dbb6 to your computer and use it in GitHub Desktop.
TLS Certificate test in powershell core
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