Powershell Invoke-RestMethod“无法连接到远程服务器”

Jim*_*Jim 6 powershell invoke-command

我编写了一个非常简单的 powershell 脚本来将 json 字符串发布到服务器。当我执行脚本时遇到一个奇怪的问题。

  1. 服务器位于我们的内网内,所有内网服务器的防火墙均已禁用。

  2. 我尝试在不同的地方执行脚本:

    • 我自己的计算机中的 Powershell 窗口(Windows 7、powershel 5)
    • 我的计算机中的 Powershell ISE
    • 在我自己的计算机上的命令提示符中启动 powershell.exe
    • 在我自己的计算机上的第三方命令提示符应用程序中启动 powershell.exe

    • Citrix seesion 中的 Powershell 窗口(Windows 10、powershel 5)

    • 在 Citrix seesion 的命令提示符中启动 powershell.exe

我检查了所有 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
Run Code Online (Sandbox Code Playgroud)

此外,我也尝试过Invoke-WebRequest,我收到了同样的错误消息。

知道为什么会发生这种情况吗?我应该用它做什么?

Mih*_*hir 5

我通过禁用/注释掉 machine.config 文件中的代理设置解决了同样的问题,我启用了该文件来拦截来自 Fiddler 中的 .Net 应用程序的网络调用。

请注意,在 machine.config 文件中进行更改后,您需要重新启动计算机。还要确保您以管理员身份运行编辑器来编辑配置文件。

小路:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config

配置元素:

配置 -> system.net -> defaultProxy


Ana*_*hag 2

几天前我遇到了类似的问题,我通过在“Invoke-WebRequest”调用中应用代理设置来解决它。

像这样的东西;

Invoke-WebRequest -URI $url -Proxy 'http://10.10.7.11:80' -ProxyCredential $creds
Run Code Online (Sandbox Code Playgroud)