忽略PowerShell Invoke-RestMethod中的自签名证书无法正常工作(又变了...)

10

在使用标准解决方案忽略证书验证后,Invoke-RestMethod 返回:

Invoke-RestMethod : A system error occurred and has been logged.  Please try again later or contact your administrator.

我今天刚刚注意到这个故障,所以我认为它与 Powershell 更新有关。我所说的"标准解决方案"是:

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

几个月前停止工作了,在Powershell中添加了一个C#类型并正确设置回调函数(在历史中有描述)。

这是我的环境:

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.674
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.674
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

这里有一些历史资料,以免此问题因为重复而被关闭。

历史

如果您在谷歌上搜索或在StackOverflow上搜索,您会发现该问题出现了几个标准答案。然而,今天我注意到所有标准解决方案都不再起作用。

Powershell给出的标准错误是:

Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
在各个论坛上通常给出的标准答案是,在调用 Invoke-RestMethod 命令之前使用此命令:

在调用 Invoke-RestMethod 命令之前使用此命令是论坛上通常给出的标准答案:

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

但是,如果您正在使用最新版本的Windows 10/2016和Powershell,则调用Invoke-RestMethod将返回:

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

为什么会发生这种情况的解释在Huddled Masses博客上找到了可以总结如下:

将ServerCertificateValidationCallback设置为脚本块对于异步回调(在任务线程上发生的回调)不起作用,因为其他线程没有运行空间来执行该脚本。

最初,我是通过以下代码解决这个问题的:

if (-not ([System.Management.Automation.PSTypeName]"TrustAllCertsPolicy").Type)
{
    Add-Type -TypeDefinition  @"
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;
    }
}
"@
}

if ([System.Net.ServicePointManager]::CertificatePolicy.ToString() -ne "TrustAllCertsPolicy")
{
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}

但是,在Windows Server 2016上这种方法不起作用,尽管在Windows 10上运行良好。所以我根据Huddled Masses的建议,编写了以下代码来处理C#中的证书验证回调,而不是使用脚本块:

function Disable-SslVerification
{
    if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        Add-Type -TypeDefinition  @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
    private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }
    public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
    public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"@
    }
    [TrustEverything]::SetCallback()
}
function Enable-SslVerification
{
    if (([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        [TrustEverything]::UnsetCallback()
    }
}

这个方法长期以来一直运行得很好,但最近我在调用 Invoke-RestMethod 时遇到了以下错误:

Invoke-RestMethod : A system error occurred and has been logged.  Please try again later or contact your administrator.

我知道部署证书是一个正确的解决方案,但有时你只是想测试一下而不必设置一个正确的PKIX。


1
如果有什么变化,他们没有更新文档。在示例2中仍然有[System.Net.ServicePointManager] :: ServerCertificateValidationCallback = {$ true}。https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1 - Tim Haintz
2
只是一点小提示,但是在PowerShell 6.0.0-beta7中已经基本解决了此问题,通过为Invoke-WebRequestInvoke-RestMethod添加一个-SkipCertificateCheck参数。显然这对于PowerShell 5.1及更早版本没有帮助,但它应该在未来的版本中得到解决。 - Bacon Bits
我认为我已经将问题隔离到我正在尝试使用的Web服务中。我认为Microsoft的东西仍在工作; 但是,Microsoft建议在脚本块中执行验证检查的修复方法绝对不起作用。 - petrsnd
1个回答

8

我认为问题出在我所调用的网络服务上面,唉!

我在我的问题中列出的 Disable-SslVerificationEnable-SslVerification 函数仍然是最好的选择,并且似乎起了作用。

我期待着Bacon Bits在评论中提到的-SkipCertificateCheck开关。那样我们就可以停止黑客行为了。=)

希望这个问题对那些试图解决相同问题但遇到“发送时发生意外错误”的人们有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接