Last active
June 7, 2017 14:31
-
-
Save BrandonStiff/f44de96d0e02058d2a6a07bad086b130 to your computer and use it in GitHub Desktop.
Set-SslCertificateValidation
This file contains 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 Set-SslCertificateValidation | |
{ | |
<# | |
.SYNOPSIS | |
This function enables or disables SSL Cert validation in your PowerShell session. Calling this affects SSL validation for ALL function calls in the session! | |
.PARAMETER Disable | |
If specified, validation is disabled. If not specified (the default) validation is re-enabled. | |
.EXAMPLE | |
Set-SslCertificateValidation -Disable | |
# Disables SSL Cert validation | |
.EXAMPLE | |
Set-SslCertificateValidation | |
# Re-enables SSL Cert validation again | |
#> | |
param | |
( | |
[switch] $Disable | |
) | |
$type = [AppDomain]::CurrentDomain.GetAssemblies().ExportedTypes | Where-Object { $_.Name -ieq "TrustAllCertsPolicy" } | |
if ( !$type ) | |
{ | |
# Disable SSL Certificate validation: | |
Add-Type -TypeDefinition @" | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsPolicy : ICertificatePolicy | |
{ | |
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem) | |
{ | |
return true; | |
} | |
} | |
"@ | |
} | |
if ( $Disable ) | |
{ | |
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy | |
} | |
else | |
{ | |
[System.Net.ServicePointManager]::CertificatePolicy = $null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment