Functions/SSL/Get-PublicKey.ps1
function Get-PublicKey { PARAM ( [Parameter(Mandatory=$true)] [Uri] $Uri, [Parameter(Mandatory=$false)] [string] $OutputFilepath ) $request = [System.Net.HttpWebRequest]::Create($uri) try { #Make the request but ignore (dispose it) the response, since we only care about the service point $request.GetResponse().Dispose() } catch [System.Net.WebException] { if ($_.Exception.Status -eq [System.Net.WebExceptionStatus]::TrustFailure) { # We ignore trust failures, since we only want the certificate, and the service point is still populated at this point } else { # Let other exceptions bubble up, or write-error the exception and return from this method throw } } #The ServicePoint object should now contain the Certificate for the site. $servicePoint = $request.ServicePoint $certificate = $servicePoint.Certificate if($OutputFilepath){$OutputFile = New-Item -Path $OutputFilepath} if ($OutputFile) { $certBytes = $certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert) [System.IO.File]::WriteAllBytes( $OutputFile, $certBytes ) $OutputFile.Refresh() } else {$certificate} } |