0
votes

I am trying to access Foreman API in Powershell version 5.1 using Invoke-RestMethod

I get following error:

Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

I tried following command before the this command but it didn't work.

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

I tried with Tls, Tls11 also but it didn't work.

1
What version of Windows are you running on?Mathias R. Jessen

1 Answers

0
votes

Are you connecting to an untrusted SSL certificate by any chance? You probably need to ignore trust errors as well as set the correct TLS version to use.

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