CertUtil.psm1

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Net") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Security.Cryptography.X509Certificates") 
<#
.Synopsis
    About this function
 
.DESCRIPTION
    Description of function
 
    Thanks to Vadims Podãns --> https://www.sysadmins.lv/blog-en/test-remote-web-server-ssl-certificate.aspx
 
.PARAMETER p1
   p1
 
.PARAMETER p2
   p2
 
.OUTPUTS
   Output information
#>

function Test-WebCertificate {
    [CmdletBinding()]
    [Alias()]
    Param
    (
        # URI List
        [Parameter(Mandatory = $false, 
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true, 
            ValueFromRemainingArguments = $true)]
        [string[]]$url = @(),
        [Parameter(Mandatory = $false, 
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true, 
            ValueFromRemainingArguments = $true)]
        [string]$path,
        [Parameter(Mandatory = $false, 
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true, 
            ValueFromRemainingArguments = $true)]
        [bool]$update = $false
    )

    Begin {
        $return = @()
        $formatDateTime = (Get-Culture).DateTimeFormat.ShortDatePattern + " " + (Get-Culture).DateTimeFormat.LongTimePattern
        if ($path -ne "") {
            if ((Test-Path $path)) {
                $csvObj = ConvertFrom-Csv -InputObject (get-content -Path $path) -Delimiter ';'
                $url += $csvObj.url
            }
            else {
                if ($update) {
                    New-Item -ItemType File $path
                }
                else {
                    Write-Error "Missing CSV file : $path"
                    exit
                }
            }
        }
    }

    Process {
        foreach ($u in $url) {
            try {
                # Create return object
                $certObject = New-Object -TypeName psobject -Property @{[string]'url' = ''; [string]'end' = $null; [string]'start' = $null; [string]'check' = ''; [string]'name' = ''; [string]'issuer' = ''; [string]'san' =''; [string]'thumbprint' = '';}

                $connString = "https://$u" 

                $null = Invoke-WebRequest -Uri $connString -TimeoutSec 3 -ErrorAction SilentlyContinue -Method Get
                $servicePoint = [System.Net.ServicePointManager]::FindServicePoint($connString)
                try {
                    $cert = [Security.Cryptography.X509Certificates.X509Certificate2]$servicePoint.Certificate
                    $certHandle = [Security.Cryptography.X509Certificates.X509Certificate2]$servicePoint.Certificate.Handle
                    #$SAN = ($certHandle.Extensions | Where-Object {$_.Oid.Value -eq "2.5.29.17"}).Format(0) -split ", "
                    $SAN = ($certHandle.Extensions | Where-Object {$_.Oid.Value -eq "2.5.29.17"}).Format(0)
                }
                catch {
                    $SAN = $null
                }
                #$certObject.url = $connString
                $certObject.url = $u
                $certObject.end = $cert.GetExpirationDateString()
                $certObject.start = $cert.GetEffectiveDateString()
                $certObject.check = get-date -Format "$formatDateTime"
                $certObject.name = $cert.Subject
                $certObject.issuer = $cert.Issuer
                $certObject.san = $SAN
                $certObject.thumbprint = $cert.Thumbprint
                
                $return += $certObject
            }
            catch {
                $certObject.url = $u
                $certObject.end = $null
                $certObject.start = $null
                $certObject.check = get-date -Format "$formatDateTime"
                $certObject.name = "Error $($_.Exception.Message)"
                $certObject.issuer = ""
                $certObject.san = ""
                $certObject.thumbprint = ""
                
                $return += $certObject
            }
        }
    }
    End {
        if ($update) {
            $return | Select-Object url, start, end, check, name, issuer, san, thumbprint | Export-Csv -Path $path -Delimiter ';' -NoTypeInformation -Encoding UTF8 #-Append
        } else {
            return $return | Select-Object url, start, end, check, name, issuer, san, thumbprint 
        }
    }
}
Export-ModuleMember -Function Test-WebCertificate