0

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? Commented Jul 16, 2020 at 13:21

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but it didn't work. I am getting a different error: The underlying connection was closed: An unexpected error occurred on a send.
Any chance that you are trying to connect via a Proxy Server? If so, try running this as soon as you open a new PowerShell session, then try the Invoke. Access web using Powershell and Proxy
The same code is working with Powershell Core 6 with -SkipCertificateCheck parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.