【问题标题】:PowerShell SSL/TLS error with Invoke-RestMethod commandPowerShell SSL/TLS 错误与 Invoke-RestMethod 命令
【发布时间】:2020-11-06 02:01:59
【问题描述】:

我正在尝试使用 Invoke-RestMethod 在 Powershell 5.1 版中访问 Foreman API

我收到以下错误:

Invoke-RestMethod : 底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。

我在此命令之前尝试了以下命令,但没有成功。

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

我尝试过使用 Tls,Tls11 也尝试过,但没有成功。

【问题讨论】:

  • 您在哪个版本的 Windows 上运行?

标签: powershell ssl


【解决方案1】:

您是否正在连接到不受信任的 SSL 证书? 您可能需要忽略信任错误并设置要使用的正确 TLS 版本。

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

【讨论】:

  • 我试过了,但没用。我收到另一个错误:底层连接已关闭:发送时发生意外错误。
  • 您是否尝试通过代理服务器进行连接?如果是这样,请尝试在打开新的 PowerShell 会话后立即运行它,然后尝试调用。 Access web using Powershell and Proxy
  • 相同的代码正在使用带有 -SkipCertificateCheck 参数的 Powershell Core 6。
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多