DevOps | Scripts | Automation

PowershellTroubleShooting

How to resolve : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel ?

“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel” error generates when there could be certificate issue or the SSL / TLS versions are not supported (version 1.0) or outdated by the request Invoke-Webrequest or Invoke-RestMethod command in that case you can use the below command resolve the above error.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

You can also use the below command,

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

You can also add certificate type class to Trust all certificate policies.

add-type @"
    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;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Any of the commands would work.