PowerShell 7x NetCore ServicePoint SSL Expiry

Hello! In 5.1 you could/can successfully use the well documented method to pull a certificate expiry date from a web request.

$request = Invoke-WebRequest $domainCheck -usebasicparsing -TimeoutSec 20 -ErrorAction Stop
$servicePoint = [Net.ServicePointManager]::FindServicePoint("$domainCheck")
$end = Get-Date $servicePoint.Certificate.GetExpirationDateString()

On netCore or PWSH 7 this doesn’t work, and is documented in a few github threads.

I don’t speak their language unfortunately so i have no idea how to implement their fixes in PowerShell. Can anyone advise?

I’ve been looking at this on and off since you’ve posted and haven’t found a way to implement the callback method. I did find an example or two but they didn’t work for me.

Just thought I’d post what I’ve come up with so far in case it helps you or someone else come up with a full solution. It seems to work in so far as no errors are thrown but I cannot get any output from the callback method. I don’t think anything is actually being passed to the callback method…

Using namespace System.Net.Http
Using namespace System.Security.Cryptography.X509Certificates
Using namespace System.Net.Security

function ServerCertificateCustomValidation {

    param (
        [HttpRequestMessage]$requestMessage, 
        [X509Certificate2]$certificate, 
        [X509Chain]$chain, 
        [SslPolicyErrors]$sslErrors
    )

    $uri = $($requestMessage.RequestUri)
    return [SslPolicyErrors].None

}

$handler = New-Object -TypeName System.Net.Http.HttpClientHandler

$handler.ServerCertificateCustomValidationCallback = ServerCertificateCustomValidation

$client  = New-Object -TypeName System.Net.Http.HttpClient -ArgumentList $handler

try {

    $response = $client.GetAsync('https://www.powershell.org')
    $statusCode = $response.Result.EnsureSuccessStatusCode()
    $responseBody = $response.Result.Content.ReadAsStringAsync()

    $handler.Dispose()
    $client.Dispose()

}
catch {
    Write-Warning "Exception Caught."
    Write-Warning $_.Exception.Message
}

Write-Host $uri