Private-Ignore-SSLErrors.ps1

Function Ignore-SSLErrors
{
    <#
    .SYNOPSIS
        Ignore SSL errors during the requests
 
    .DESCRIPTION
        supporting function. workaround for the errors like Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS
        secure channel.
         
 
    .NOTES
        Author: ?
 
    .LINK
        http://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
    #>
    

    [CmdletBinding(
    )]
    Param()
    
    Begin{}
    
    Process
    {
    $Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler = $Provider.CreateCompiler()
    $Params = New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable = $false
    $Params.GenerateInMemory = $true
    $Params.IncludeDebugInformation = $false
    $Params.ReferencedAssemblies.Add("System.DLL") > $null
    $TASource=@'
        namespace Local.ToolkitExtensions.Net.CertificatePolicy
        {
            public class TrustAll : System.Net.ICertificatePolicy
            {
                public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
                {
                    return true;
                }
            }
        }
'@
 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    ## We create an instance of TrustAll and attach it to the ServicePointManager
    $TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
    }
    
    End{}                
}