26

似乎已经提出并回答了这个问题,但到目前为止,我遇到的每个解决方案都无济于事。我正在编写一个 PowerShell 脚本来运行一些 REST API 来获取使用信息。我的脚本立即中断只是试图与服务器通信。为了测试,我正在做一个非常简单的命令:

Invoke-RestMethod 'https://server:4443/login'

它返回此错误:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.

我可以运行相同的命令,但使用 URL google.com 并且我得到了有效的返回,所以我知道该命令一般来说是有效的。

如果我在服务器本身上运行 curl 等效项,则事情会按预期完成。这是 curl 命令的详细输出的片段:

* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.0 / DHE-RSA-AES256-SHA
* Server certificate:
*        subject: CN=localhost
*        start date: 2016-03-22 21:48:57 GMT
*        expire date: 2026-03-20 21:48:57 GMT
*        issuer: CN=localhost
*        SSL certificate verify result: self signed certificate (18), continuing anyway.

我只是假设这是一个基于搜索相当通用的错误 PowerShell 返回的自签名证书问题。

我试过了:

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

和其他类似的方法(复杂的功能)来帮助忽略证书问题而没有运气。

我正在运行 PowerShell 5,以防万一。

我对 PowerShell 代码很满意,但这是我第一次尝试 Invoke-RestMethod,所以也许我遗漏了一些东西。任何见解都值得赞赏。

4

5 回答 5

23

这也适用于带有 invoke-restmethod/webrequest 的更高版本的 powershell。它通过将处理程序实现为本机 .net 来避免对运行空间的要求:

if (-not("dummy" -as [type])) {
    add-type -TypeDefinition @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public static class Dummy {
    public static bool ReturnTrue(object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }

    public static RemoteCertificateValidationCallback GetDelegate() {
        return new RemoteCertificateValidationCallback(Dummy.ReturnTrue);
    }
}
"@
}

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [dummy]::GetDelegate()

希望这可以帮助。

于 2017-09-06T05:02:45.313 回答
11

如果在@x0n 回答之后,您仍然有问题,请尝试在 Request/Rest 之前添加

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

为我工作的脚本:

if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
    using System;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    public class ServerCertificateValidationCallback
    {
        public static void Ignore()
        {
            if(ServicePointManager.ServerCertificateValidationCallback ==null)
            {
                ServicePointManager.ServerCertificateValidationCallback += 
                    delegate
                    (
                        Object obj, 
                        X509Certificate certificate, 
                        X509Chain chain, 
                        SslPolicyErrors errors
                    )
                    {
                        return true;
                    };
            }
        }
    }
"@
    Add-Type $certCallback
 }

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
[ServerCertificateValidationCallback]::Ignore()

Invoke-WebRequest https://*YOUR URI*
于 2020-01-04T17:05:49.350 回答
8

我知道这是旧的,但是当我有这个问题没有实际检查时它仍然出现。谷歌第一对吗?

尝试这个:

invoke-restMethod -SkipCertificateCheck -uri 'https://server:4443/login' -etc..etc..etc..

通过谷歌得到它: https ://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-6

于 2019-12-20T13:26:09.213 回答
0

我有一个类似的问题,我的 PS 版本是 5.1,我最初使用[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

但以下任何内容Powershell6请使用以下链接中的证书策略: https ://gist.github.com/rcaganti/aae721ebccd25eaab0b8b3dd67ad4b9b

它就像魅力一样。感谢拉维查甘蒂。

于 2020-04-14T13:20:34.553 回答
0

我一直在与同样的问题作斗争一段时间。我尝试了这些脚本的几种变体,就像这个线程中已经发布的那样。我惊讶地发现,通过使用相同的脚本并在我自己的工作站上运行它而不是在我的测试环境中运行它。

最后,我发现只需使用提升的凭据(以管理员身份运行)在我的测试环境中运行脚本,它就可以在没有 SSL/TLS 错误的情况下工作。

于 2020-03-27T15:12:34.123 回答