在使用标准解决方案忽略证书验证后,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。
Invoke-WebRequest
和Invoke-RestMethod
添加一个-SkipCertificateCheck
参数。显然这对于PowerShell 5.1及更早版本没有帮助,但它应该在未来的版本中得到解决。 - Bacon Bits