【问题标题】:Invoke-RestMethod - Ignore Self Signed CertsInvoke-RestMethod - 忽略自签名证书
【发布时间】:2016-07-27 03:35:38
【问题描述】:

似乎已经提出并回答了这个问题,但到目前为止,我遇到的每个解决方案都无济于事。我正在编写一个 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 命令的详细输出的 sn-p:

* 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,所以也许我遗漏了一些东西。任何见解都值得赞赏。

【问题讨论】:

  • 如果网络服务器需要身份验证 (-Credential),您可能会错过您的凭据。或者您需要指定-Method 参数post 或`get。很难说我们是否不知道您的服务器在期待什么。
  • 我很抱歉没有预先提供完整的信息。我通过标头传递凭据。我也尝试过显式调用该方法。

标签: rest powershell self-signed powershell-5.0


【解决方案1】:

我也遇到过类似的问题,我的 PS 版本是 5.1,我最初用的是[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

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

它就像魅力一样。感谢 Ravi Chaganti。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      [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*
      

      【讨论】:

      • 这适用于我在 Windows Server 2016 Box + PowerShell 5 (PSVersion 5.1.14393.693) 对于 PowerShell 6+ 使用 Invoke-WebRequest 的 -SkipCertificateCheck 标志
      • 还可以寻找 stackoverflow.com/a/55246366/1358289 以获得针对此问题的纯 powershell 解决方案
      • 感谢您添加完整示例以了解魔术代码的放置位置。
      【解决方案4】:

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

      试试这个:

      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

      【讨论】:

      • 问题标记为 5.0,您所指的文档是版本 6。
      【解决方案5】:

      这也将在更高版本的 powershell 中使用 invoke-restmethod/webrequest。它通过将处理程序实现为原生 .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()
      

      希望这会有所帮助。

      【讨论】:

      • 工作就像一个魅力。建议的答案可能更完整,但需要维护更多代码。您的解决方案非常有效。谢谢
      • 以上对我来说效果很好,但此外我还必须在这个答案中添加代码:stackoverflow.com/a/41618979/908316
      • 哇,它有效。这比在 cURL 中添加“--insecure”要容易得多。感谢 PowerShell 让我们的生活变得如此轻松。对不起,我大声说出来了吗? ;p
      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 2017-04-16
      • 2012-08-30
      • 2011-01-16
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多