Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active March 21, 2025 14:18
Show Gist options
  • Select an option

  • Save IISResetMe/66ab3f0ced4eb406f21bf354cfe7ad45 to your computer and use it in GitHub Desktop.

Select an option

Save IISResetMe/66ab3f0ced4eb406f21bf354cfe7ad45 to your computer and use it in GitHub Desktop.

Revisions

  1. IISResetMe revised this gist Apr 2, 2020. 1 changed file with 51 additions and 15 deletions.
    66 changes: 51 additions & 15 deletions s_client.ps1
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,53 @@
    $uri = [uri]'https://www.powershellgallery.com/api/v2/package/Pester/4.10.1'

    $client = [System.Net.Sockets.TcpClient]::new($uri.Authority, 443)
    try{
    $tlsClient = [System.Net.Security.SslStream]::new($client.GetStream())
    $tlsClient.AuthenticateAsClient($uri.Authority)

    [System.Security.Cryptography.X509Certificates.X509Certificate2]$tlsClient.RemoteCertificate
    using namespace System.Net.Sockets
    using namespace System.Net.Security
    using namespace System.Security.Cryptography.X509Certificates

    function ConvertFrom-X509Certificate {
    param(
    [Parameter(ValueFromPipeline)]
    [X509Certificate2]$Certificate
    )

    process {
    @(
    '-----BEGIN CERTIFICATE-----'
    [Convert]::ToBase64String(
    $Certificate.Export([X509ContentType]::Cert),
    [Base64FormattingOptions]::InsertLineBreaks
    )
    '-----END CERTIFICATE-----'
    ) -join [Environment]::NewLine
    }
    }
    finally{
    if($tlsClient -is [System.IDisposable]){
    $tlsClient.Dispose()
    }
    elseif($tcpClient -is [System.IDisposable]){
    $tcpClient.Dispose()
    }

    function Get-RemoteCertificate {
    param(
    [Alias('CN')]
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$ComputerName,

    [Parameter(Position = 1)]
    [UInt16]$Port = 443,

    [ValidateSet('Base64', 'X509Certificate')]
    [string]$As = 'X509Certificate'
    )

    $tcpClient = [TcpClient]::new($ComputerName, $Port)
    try {
    $tlsClient = [SslStream]::new($tcpClient.GetStream())
    $tlsClient.AuthenticateAsClient($ComputerName)

    if ($As -eq 'Base64') {
    return $tlsClient.RemoteCertificate |ConvertFrom-X509Certificate
    }

    return $tlsClient.RemoteCertificate -as [X509Certificate2]
    }
    finally {
    if ($tlsClient -is [IDisposable]) {
    $tlsClient.Dispose()
    }
    $tcpClient.Dispose()
    }
    }
  2. IISResetMe created this gist Mar 23, 2020.
    17 changes: 17 additions & 0 deletions s_client.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    $uri = [uri]'https://www.powershellgallery.com/api/v2/package/Pester/4.10.1'

    $client = [System.Net.Sockets.TcpClient]::new($uri.Authority, 443)
    try{
    $tlsClient = [System.Net.Security.SslStream]::new($client.GetStream())
    $tlsClient.AuthenticateAsClient($uri.Authority)

    [System.Security.Cryptography.X509Certificates.X509Certificate2]$tlsClient.RemoteCertificate
    }
    finally{
    if($tlsClient -is [System.IDisposable]){
    $tlsClient.Dispose()
    }
    elseif($tcpClient -is [System.IDisposable]){
    $tcpClient.Dispose()
    }
    }