<# .Synopsis Reads RDP Certificate information .DESCRIPTION This script connects to a remote host to read and return the RDP certificate information .EXAMPLE Read-RDPCert -Computer "PC1,PC2".Split(",") -Verbose Get the RDP certificate information for PC1 and PC2 .EXAMPLE Another example of how to use this cmdlet .NOTES Alan Kaplan 6/12/2017 Based on code and comments found at https://blogs.technet.microsoft.com/parallel_universe_-_ms_tech_blog/2014/06/26/~ reading-a-certificate-off-a-remote-ssl-server-for-troubleshooting-with-powershell/ #> Function Read-RDPCert { [CmdletBinding()] param( [parameter( Mandatory=$true, ValueFromPipeline=$true )] [string[]]$Computer, [parameter(Mandatory=$False)] [int]$Port = 3389 ) Begin { } Process { Foreach ($computerName in $Computer){ Try{ #Create a TCP Socket to the computer and a port number $tcpsocket = New-Object Net.Sockets.TcpClient($computerName, $port) -ErrorAction stop     #Socket Got connected get the tcp stream ready to read the certificate     Write-Verbose "Successfully Connected to $computername on $port"     $tcpstream = $tcpsocket.GetStream()     Write-Verbose "Reading SSL Certificate…."     #Create an SSL Connection    $sslStream = New-Object System.Net.Security.SslStream($tcpstream,$false, { param($sender, $certificate, $chain, $sslPolicyErrors) return $true }) #Force the SSL Connection to send us the certificate     $sslStream.AuthenticateAsClient($computerName)     #Read the certificate     $certinfo = New-Object system.security.cryptography.x509certificates.x509certificate2($sslStream.RemoteCertificate) [PSCustomObject]@{ ComputerName = $computername #DnsNameList = $certinfo.DnsNameList -join ";" #Issuer = $certinfo.Issuer Subject = $certinfo.Subject NotBefore = $certinfo.NotBefore NotAfter =$certinfo.NotAfter SerialNumber = $certinfo.SerialNumber Thumbprint = $certinfo.Thumbprint Result = 'Success' } } Catch { #If any failures, parse and return error message $err = $Error[0].Exception.Message.Split(':')[1].Replace('"','').Trim() Write-Verbose $err [PSCustomObject]@{ ComputerName = $computername #DnsNameList = '' #Issuer = '' Subject = '' NotBefore = '' NotAfter ='' SerialNumber = '' Thumbprint = '' Result = $Err } } }#end For }#end Process End { } }