Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BrandonStiff
Last active June 7, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrandonStiff/f44de96d0e02058d2a6a07bad086b130 to your computer and use it in GitHub Desktop.
Save BrandonStiff/f44de96d0e02058d2a6a07bad086b130 to your computer and use it in GitHub Desktop.
Set-SslCertificateValidation
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