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

问题描述 投票:0回答:3

我编写了一个非常简单的 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

此外,我也尝试了

Invoke-WebRequest
,我收到了同样的错误消息。

知道为什么会发生这种情况吗?我该怎么办?

powershell invoke-command
3个回答
4
投票

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

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

路径:

C:\Windows\Microsoft.NET\Framework64 4.0.30319\Config\machine.config

配置元素:

配置 -> system.net -> defaultProxy


2
投票

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

类似这样的东西;

Invoke-WebRequest -URI $url -Proxy 'http://10.10.7.11:80' -ProxyCredential $creds

0
投票

我也遇到了同样的问题:

Invoke-RestMethod : Unable to connect to the remote server.
基本上是一些网络问题。

  • 我刚刚从 ms-store 安装了一个免费 VPN(热点盾),然后我的问题(安装 scoop)就解决了。
© www.soinside.com 2019 - 2024. All rights reserved.