# PowerShell Script to Extract SSL Certificate from a Remote Server (LDAPS)
# Define server and port
$server = "fggg"
$port = 636
# Connect and retrieve the certificate
$tcpClient = New-Object System.Net.Sockets.TcpClient
$tcpClient.Connect($server, $port)
$sslStream = New-Object System.Net.Security.SslStream($tcpClient.GetStream(), $false, ({$true}))
$sslStream.AuthenticateAsClient($server)
# Export the certificate
$remoteCert = $sslStream.RemoteCertificate
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $remoteCert
$certPath = "$env:USERPROFILE\Desktop\$server.cer"
[System.IO.File]::WriteAllBytes($certPath, $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert))
# Clean up
$sslStream.Close()
$tcpClient.Close()
Write-Host "Certificate saved to Desktop: $certPath"