【问题标题】:Could not establish trust relationship for the SSL/TLS secure channel with Invoke-Webrequest from PowerShell无法使用来自 PowerShell 的 Invoke-Webrequest 为 SSL/TLS 安全通道建立信任关系
【发布时间】:2018-02-21 13:56:12
【问题描述】:

当我尝试在 https 上使用 Invoke-WebRequest 时,我遇到了一些奇怪的错误:

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

这是我的代码:

   [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls
    $url = "https://X.X.X.X:4343/officescan/console/html/cgi/cgiChkMasterPwd.exe"
    $r = Invoke-WebRequest $url -SessionVariable office

对我有什么建议吗??非常感谢。

【问题讨论】:

标签: powershell ssl


【解决方案1】:

当然这是无效证书的问题(自动签名?过期?) 如果使用 Powershell 7+,只需使用新参数

Invoke-WebRequest -Uri 'https://trees.com/' -SkipCertificateCheck

如果使用 Powershell 5.1 会有点困难:

$code= @"
        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;
            }
        }
"@
Add-Type -TypeDefinition $code -Language CSharp
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Invoke-WebRequest -Uri 'https://trees.com/'

【讨论】:

    【解决方案2】:

    如果您在 Uri 中使用 https:// 并且 URL 可通过 http:// 获得,您也会遇到该问题。更具体地说,使用

    Invoke-WebRequest -Uri 'https://trees.com/'
    

    会报如下错误

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

    那么,如果你使用

    Invoke-WebRequest -Uri 'trees.com'
    

    或者如果你使用

    Invoke-WebRequest -Uri 'http://trees.com'
    

    可以正常使用

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多