Skip to content

Instantly share code, notes, and snippets.

@mcx808
Created July 27, 2022 14:51
Show Gist options
  • Save mcx808/8fa0b3ec64fb456a637048eaf89ed661 to your computer and use it in GitHub Desktop.
Save mcx808/8fa0b3ec64fb456a637048eaf89ed661 to your computer and use it in GitHub Desktop.
Gets a remote TLS certificate in Powershell 6/7
function Get-RemoteCertificate {
[CmdletBinding()]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate])]
param (
[Parameter(
Mandatory,
ValueFromPipeline
)]
[ValidateNotNull()]
[Uri]
$Uri
)
process {
try {
$TcpClient = [System.Net.Sockets.TcpClient]::new($Uri.Host, $Uri.Port)
try {
$SslStream = [System.Net.Security.SslStream]::new($TcpClient.GetStream())
$SslStream.AuthenticateAsClient($Uri.Host)
$SslStream.RemoteCertificate
} finally {
$SslStream.Dispose()
}
} finally {
$TcpClient.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment