Skip to content

Instantly share code, notes, and snippets.

@tcartwright
Last active May 22, 2025 14:50
Show Gist options
  • Select an option

  • Save tcartwright/1cda5676a81a4cad49c1e33c4897c3eb to your computer and use it in GitHub Desktop.

Select an option

Save tcartwright/1cda5676a81a4cad49c1e33c4897c3eb to your computer and use it in GitHub Desktop.

Revisions

  1. tcartwright revised this gist May 22, 2025. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@ $servicePoint = $req.ServicePoint

    $props = $cert | Select-Object @{Name="Address"; Expression={$servicePoint.Address}},
    Subject,
    @{Name="TlsVersion"; Expression={$servicePoint.ProtocolVersion}},
    @{Name="ValidFrom"; Expression={$_.NotBefore}},
    @{Name="ValidTo"; Expression={$_.NotAfter}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
  2. tcartwright revised this gist May 22, 2025. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions TestHttpsCertAndTls.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #Requires -Version 7.0

    Clear-Host

    $hostName = "www.microsoft.com"
    $port = 443

    $tcpClient = New-Object System.Net.Sockets.TcpClient

    try {
    $tcpClient.Connect($hostName, $port)
    $sslStream = New-Object System.Net.Security.SslStream($tcpClient.GetStream(), $false, ({ $true }))

    try {
    $sslStream.AuthenticateAsClient($hostName)
    $tlsVersion = $sslStream.SslProtocol
    } catch {
    Write-Error "TLS handshake failed: $_"
    $tcpClient.Close()
    return
    }

    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($sslStream.RemoteCertificate)
    $sslStream.Close()
    $tcpClient.Close()

    # Parse SAN (DNS names) from extensions
    $sanExtension = $cert.Extensions | Where-Object { $_.Oid.FriendlyName -eq "Subject Alternative Name" }
    $dnsNames = if ($sanExtension) { $sanExtension.Format($false) } else { "N/A" }

    $props = [PSCustomObject]@{
    Address = $hostName
    TlsVersion = $tlsVersion
    Subject = $cert.Subject
    ValidFrom = $cert.NotBefore
    ValidTo = $cert.NotAfter
    IsValid = $cert.Verify() -and (Get-Date) -le $cert.NotAfter
    Encryption = $cert.SignatureAlgorithm.FriendlyName
    DnsNames = $dnsNames
    Issuer = $cert.Issuer
    }

    $props | Format-List
    } catch {
    Write-Error "Connection failed: $_"
    }
  3. tcartwright revised this gist May 22, 2025. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,15 @@ $hostName = "www.microsoft.com"

    $req = [System.Net.HttpWebRequest]::Create("https://$hostName")
    $req.GetResponse().Dispose()
    [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $req.ServicePoint.Certificate
    #$cert | Format-List *
    $servicePoint = $req.ServicePoint
    [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $servicePoint.Certificate

    $props = $cert | Select-Object Subject,
    # $servicePoint | Format-List *
    # $cert | Format-List *

    $props = $cert | Select-Object @{Name="Address"; Expression={$servicePoint.Address}},
    Subject,
    @{Name="TlsVersion"; Expression={$servicePoint.ProtocolVersion}},
    @{Name="ValidFrom"; Expression={$_.NotBefore}},
    @{Name="ValidTo"; Expression={$_.NotAfter}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
    @@ -16,3 +21,7 @@ $props = $cert | Select-Object Subject,
    Issuer

    $props | Out-Host




  4. tcartwright revised this gist Feb 20, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -5,14 +5,14 @@ $hostName = "www.microsoft.com"
    $req = [System.Net.HttpWebRequest]::Create("https://$hostName")
    $req.GetResponse().Dispose()
    [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $req.ServicePoint.Certificate
    #$cert | Format-List *

    $props = $cert | Select-Object Subject,
    @{Name="ValidFrom"; Expression={$_.NotBefore}},
    @{Name="ValidTo"; Expression={$_.NotAfter}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
    @{Name="Encryption"; Expression={$_.SignatureAlgorithm.FriendlyName}},
    @{Name="DnsNames"; Expression={$_.DnsNameList.Unicode}},
    Issuer

    $props | Out-Host

    #$cert | Format-List *
  5. tcartwright revised this gist Feb 20, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ $props = $cert | Select-Object Subject,
    @{Name="ValidFrom"; Expression={$_.NotBefore}},
    @{Name="ValidTo"; Expression={$_.NotAfter}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
    @{Name="EncryptionLevel"; Expression={$_.SignatureAlgorithm.FriendlyName}},
    @{Name="Encryption"; Expression={$_.SignatureAlgorithm.FriendlyName}},
    Issuer

    $props | Out-Host
  6. tcartwright revised this gist Feb 20, 2025. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,12 @@ $req.GetResponse().Dispose()
    [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $req.ServicePoint.Certificate

    $props = $cert | Select-Object Subject,
    @{Name="ValidFrom"; Expression={$_.GetEffectiveDateString()}},
    @{Name="ValidTo"; Expression={$_.GetExpirationDateString()}},
    @{Name="ValidFrom"; Expression={$_.NotBefore}},
    @{Name="ValidTo"; Expression={$_.NotAfter}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
    @{Name="EncryptionLevel"; Expression={$_.SignatureAlgorithm.FriendlyName}},
    Issuer

    $props | Format-Table
    $props | Out-Host

    #$cert | Format-List *
  7. tcartwright revised this gist Feb 19, 2025. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,12 @@ $hostName = "www.microsoft.com"

    $req = [System.Net.HttpWebRequest]::Create("https://$hostName")
    $req.GetResponse().Dispose()
    $cert = $req.ServicePoint.Certificate
    [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert = $req.ServicePoint.Certificate

    $props = $cert | Select-Object Subject,
    @{Name="ValidFrom"; Expression={$_.GetEffectiveDateString()}},
    @{Name="ValidTo"; Expression={$_.GetExpirationDateString()}},
    @{Name="IsValid"; Expression={$_.Verify() -and (Get-Date) -le ([DateTime]::Parse($_.GetExpirationDateString())) }},
    Issuer

    $props | Format-Table
    $props | Format-Table
  8. tcartwright created this gist Feb 19, 2025.
    14 changes: 14 additions & 0 deletions TestHttpsCert.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    Clear-Host

    $hostName = "www.microsoft.com"

    $req = [System.Net.HttpWebRequest]::Create("https://$hostName")
    $req.GetResponse().Dispose()
    $cert = $req.ServicePoint.Certificate

    $props = $cert | Select-Object Subject,
    @{Name="ValidFrom"; Expression={$_.GetEffectiveDateString()}},
    @{Name="ValidTo"; Expression={$_.GetExpirationDateString()}},
    Issuer

    $props | Format-Table