Powershell Invoke-RestMethod "Unable to connect to the remote server"
Asked Answered
C

3

6

I write a very simple powershell script to post a json string to server. I have a strange issue when I execute the script.

  1. The server is inside our intranet, the firewall is disable for all intranet server.

  2. I tried to execute the script in different places:

    • Powershell window in my own computer (windows 7, powershel 5)
    • Powershell ISE in my computer
    • start powershell.exe in a command prompt in my own computer
    • start powershell.exe in a third party command prompt application in my own computer

    • Powershell window in a citrix seesion (windows 10, powershel 5)

    • start powershell.exe in a command prompt in a citrix seesion

I checked all of 6 places have ExecutionPolicy "RemoteSigned",

But, I can only successfully execute the script in last 3 places,

In first 3 place, I get an error

 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

Besides, I also tried Invoke-WebRequest, I got same error message.

Any idea why this is happen? What should I do with it?

Cathycathyleen answered 8/8, 2016 at 13:47 Comment(7)
which powershell version is installed on remote server?Mcgill
Please edit your code or a minimal reproducible example into your question.Hardner
On the remote server is version 4 (win server 2012r2)Cathycathyleen
@Dheeraj Roy : what does the PS version of the remote server has to do with this when REST (so plain HTTP) is being used to communicate to the server? I really miss the logic here (I work a lot with PS and REST). Question for Jim : if you try to use Invoke-RestMethod from the console host (so without using a script) does it work?Sunshinesunspot
Tried in place 3, and 4. Only works in place 4, not in place 3. I think I have some configuration prevent me to use Invoke-RestMethod in some consoles. I just do not know which ones..Cathycathyleen
Today I use Fiddler to debug, if I have Fiddler open, I can execute my script to POST in First 3 places. So what Fiddler does to help in this case?Cathycathyleen
In command prompt, I can POST to the server using curl.exe. If I enter powershell mode, I couldn't do that using Invoke-RestMethod to the same server :(Cathycathyleen
B
5

I solved same problem by disabling / commenting out proxy settings in the machine.config file which I enabled to intercept network calls from my .Net application in Fiddler.

Please note that you need to restart your computer after you make change in machine.config file. And also make sure you run editor as Administrator to edit config file.

Path:

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

Configuration Element:

configuration -> system.net -> defaultProxy

Betray answered 3/10, 2017 at 14:41 Comment(1)
I was too lazy to do this so I just opened Fiddler to make it work. Funny that we all seem to have this same problem caused by Fiddler :)Emee
O
2

I had similar issue few days back and I resolved it by applying the proxy settings in the "Invoke-WebRequest" call.

Something like this;

Invoke-WebRequest -URI $url -Proxy 'http://10.10.7.11:80' -ProxyCredential $creds
Olathe answered 30/3, 2017 at 13:8 Comment(1)
proxy param is needed behind a cooperate firewall, right? i have my own pc, so no proxy.Sternutatory
G
1

I had the same problem: Invoke-RestMethod : Unable to connect to the remote server. basically its some network problem.

  • I just installed a free-VPN from ms-store(hotspot shield), and then my problem(installing scoop) resolved like nothing.
Glialentn answered 26/12, 2023 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.