4

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?

techguy1029
  • 743
  • 10
  • 29
Jim
  • 769
  • 3
  • 10
  • 22
  • which powershell version is installed on remote server? – Dheeraj Kumar Aug 08 '16 at 13:48
  • Please [edit] your code or a [mcve] into your question. – briantist Aug 08 '16 at 13:53
  • On the remote server is version 4 (win server 2012r2) – Jim Aug 08 '16 at 13:57
  • @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? – bluuf Aug 08 '16 at 14:08
  • 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.. – Jim Aug 08 '16 at 14:27
  • 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? – Jim Aug 09 '16 at 07:56
  • 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 :( – Jim Aug 09 '16 at 08:44

2 Answers2

3

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

Mihir
  • 419
  • 3
  • 10
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
AnandShanbhag
  • 6,389
  • 1
  • 20
  • 20
  • proxy param is needed behind a cooperate firewall, right? i have my own pc, so no proxy. – Timo Mar 25 '21 at 16:38