似乎已经提出并回答了这个问题,但到目前为止我遇到的每个解决方案都无济于事。我正在编写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,所以也许我错过了一些东西。任何见解都表示赞赏。
答案 0 :(得分:14)
这也可以在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()
希望这有帮助。
答案 1 :(得分:4)
如果在@ x0n回答后仍然存在问题,请尝试在“请求/保留此”之前添加
[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*
答案 2 :(得分:1)
我一直在研究同样的问题,我找到的最好的资源就是这篇博文:
http://huddledmasses.org/blog/validating-self-signed-certificates-properly-from-powershell/
答案 3 :(得分:0)
我知道这很老,但是当我有这个问题而没有实际检查时,它仍然出现。 google优先吗?
尝试一下:
invoke-restMethod -SkipCertificateCheck -uri 'https://server:4443/login' -etc..etc..etc..
通过Google在这里得到它: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-6
答案 4 :(得分:0)
我已经在同一个问题上斗争了一段时间了。我尝试了这种脚本的几种变体,例如此线程中已经发布的脚本。通过使用相同的脚本并在自己的工作站上而不是在可以正常工作的测试环境中运行脚本,我感到惊讶。
最后,我发现仅通过使用提升的凭据(以管理员身份运行)在测试环境中运行脚本,该脚本就可以正常工作而没有SSL / TLS错误。
答案 5 :(得分:0)
我有一个类似的问题,我的PS版本是5.1,我最初使用[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
但是Powershell6
以下的任何内容,请使用以下链接中的证书策略:
https://gist.github.com/rchaganti/aae721ebccd25eaab0b8b3dd67ad4b9b
它就像魅力。多亏Ravi Chaganti。
答案 6 :(得分:-1)
应用程序可以设置ServerCertificateValidationCallback 属性到一个方法,用于客户端的自定义验证 服务器证书。
只需添加此内容即可Invoke-RestMethod
或Invoke-WebMethod
。
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }