PowerShell Invoke-RestMethod -发送时发生意外错误



我试图从Windows 10 PowerShell发送一个简单的get请求,但我总是得到一个错误:

Invoke-RestMethod https://myiseserver/admin/API/mnt/Session/MACAddress/B8:3B:CC:50:BA:B6 -Credential $cred
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

我试图以许多不同的方式启用TLS,但错误总是相同的。我正在尝试这个:

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

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

当我用原来的CURL(不是PS别名)做同样的事情时,一切都工作得很好。

我注意到,当我设置TLS1.3时,我得到一个不同的错误

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls13
Invoke-RestMethod -Uri https://myiseserver/admin/API/mnt/Session/MACAddress/B8:3B:CC:50:BA:B6 -Credential $cred
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.

我也尝试了不同的资源,如https://google.com和3台不同的Windows 10计算机,但效果是相同的-发送时发生错误。

我最近解决了Windows server 2022中PowerShell的类似问题。

在我的例子中,它是由一个实际运行脚本的用户的代理被关闭引起的。

请注意,我没有调用外部站点,因为我从PowerShell调用的站点托管在同一台服务器的IIS中,但是PowerShell命令仍然需要打开代理。

打开代理:打开ie浏览器>设置符号>"互联网Options"比;"Connections"选项卡在"局域网settings">

标记下面两个复选框,并输入有效的代理和端口。

请注意,这些选项是基于每个用户的-如果您为一个系统帐户设置了它,另一个系统帐户可能没有配置它。

代理设置

最新更新