我编写了一个非常简单的 powershell 脚本来将 json 字符串发布到服务器。当我执行脚本时遇到一个奇怪的问题。
服务器位于我们的内网内,所有内网服务器的防火墙均已禁用。
我尝试在不同的地方执行脚本:
在我自己的计算机上的第三方命令提示符应用程序中启动powershell.exe
citrix seesion 中的 Powershell 窗口 (Windows 10、powershel 5)
我检查了所有 6 个地方都有 ExecutionPolicy“RemoteSigned”,
但是,我只能成功执行最后3个地方的脚本,
在前 3 位,我收到错误
Invoke-RestMethod : Unable to connect to the remote server
At F:\Temp\test.ps1:46 char:9
+ $resp = Invoke-RestMethod -Method POST -Body $result ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod],
WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand
此外,我也尝试了
Invoke-WebRequest
,我收到了同样的错误消息。
知道为什么会发生这种情况吗?我该怎么办?
我通过禁用/注释掉 machine.config 文件中的代理设置解决了同样的问题,我启用了该文件来拦截来自 Fiddler 中的 .Net 应用程序的网络调用。
请注意,更改 machine.config 文件后需要重新启动计算机。并且确保您以管理员身份运行编辑器来编辑配置文件。
路径:
C:\Windows\Microsoft.NET\Framework64 4.0.30319\Config\machine.config
配置元素:
配置 -> system.net -> defaultProxy
几天前我遇到了类似的问题,我通过在“Invoke-WebRequest”调用中应用代理设置解决了该问题。
类似这样的东西;
Invoke-WebRequest -URI $url -Proxy 'http://10.10.7.11:80' -ProxyCredential $creds
我也遇到了同样的问题:
Invoke-RestMethod : Unable to connect to the remote server.
基本上是一些网络问题。