Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.