无法使用来自 PowerShell 的 Invoke-Webrequest 为 SSL/TLS 安全通道建立信任关系

Sco*_*Lee 6 powershell ssl

当我尝试在 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
Run Code Online (Sandbox Code Playgroud)

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

小智 18

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

Invoke-WebRequest -Uri 'https://trees.com/' -SkipCertificateCheck
Run Code Online (Sandbox Code Playgroud)

如果使用 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/'
Run Code Online (Sandbox Code Playgroud)