Last active
June 23, 2017 10:36
-
-
Save gravejester/9247074843337eb8edbd3b2e2c108405 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-CertInfoHttp { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Position = 0, Mandatory)] | |
[string] $URL, | |
[Parameter()] | |
[switch] $ReturnCertificate | |
) | |
try { | |
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} | |
$webRequest = [System.Net.HttpWebRequest]::Create($URL) | |
$webRequest.KeepAlive = $false | |
$webRequest.Timeout = 5000 | |
$webRequest.ServicePoint.ConnectionLeaseTimeout = 5000 | |
$webRequest.ServicePoint.MaxIdleTime = 5000 | |
#$null = $webRequest.GetResponse() | |
$null = $webRequest.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 | |
{ | |
Write-Warning $_.Exception.Message | |
} | |
} | |
catch { | |
Write-Warning $_.Exception.Message | |
} | |
if (($webRequest.ServicePoint.Certificate) -and ($webRequest.ServicePoint.Certificate.Handle -ne 0)) { | |
if ($ReturnCertificate) { | |
Write-Output $webRequest.ServicePoint.Certificate | |
} | |
else { | |
Write-Output ([PSCustomObject] [Ordered] @{ | |
IssuerCN = $webRequest.ServicePoint.Certificate.Issuer.Split(', ',[System.StringSplitOptions]::RemoveEmptyEntries)[0].Split('=')[1] | |
SubjectCN = $webRequest.ServicePoint.Certificate.Subject.Split(', ',[System.StringSplitOptions]::RemoveEmptyEntries)[0].Split('=')[1] | |
ValidFrom = $webRequest.ServicePoint.Certificate.GetEffectiveDateString() | |
ValidTo = $webRequest.ServicePoint.Certificate.GetExpirationDateString() | |
}) | |
} | |
$webRequest.ServicePoint.Certificate.Dispose() | |
} | |
[Net.ServicePointManager]::ServerCertificateValidationCallback = $null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment