首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Invoke-RestMethod -忽略自签名证书

Invoke-RestMethod -忽略自签名证书
EN

Stack Overflow用户
提问于 2016-04-06 23:49:08
回答 5查看 60.2K关注 0票数 24

这个问题似乎已经被问到并得到了回答,但到目前为止,我遇到的每一个解决方案都没有帮助。我正在编写一个PowerShell脚本来运行一些REST API来获取使用信息。我的脚本在试图与服务器通信时立即中断。为了测试起见,我执行了一个非常简单的命令:

代码语言:javascript
运行
复制
Invoke-RestMethod 'https://server:4443/login'

它返回以下错误:

代码语言:javascript
运行
复制
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.

我可以运行相同的命令,但是使用URL google.com,我会得到一个有效的返回,所以我知道这个命令大体上是有效的。

如果我在服务器上运行curl等效项,事情就会如期完成。以下是curl命令的详细输出的一个片段:

代码语言:javascript
运行
复制
* 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返回的相当通用的错误的自签名证书问题。

我试过了:

代码语言:javascript
运行
复制
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

和其他类似的方法(复杂的函数),帮助忽略证书问题,但没有运气。

我正在运行PowerShell 5,以防有帮助。

我对PowerShell代码很在行,但这是我第一次尝试使用Invoke-RestMethod,所以我可能遗漏了一些东西。任何洞察力都是值得欣赏的。

EN

回答 5

Stack Overflow用户

发布于 2017-09-06 13:02:45

这也可以在powershell的后续版本中使用invoke-restmethod/webrequest。它通过将处理程序实现为本机.net来避免对运行空间的要求:

代码语言:javascript
运行
复制
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()

希望这能有所帮助。

票数 23
EN

Stack Overflow用户

发布于 2020-01-05 01:05:49

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

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

我的工作脚本:

代码语言:javascript
运行
复制
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*
票数 11
EN

Stack Overflow用户

发布于 2019-12-20 21:26:09

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

试试这个:

代码语言:javascript
运行
复制
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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36456104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档