Powershell v5.1 Invoke-RestMethod and bypass proxy

21.4k Views Asked by At

I am currently in Powershell V5.1 and would like to bypass Internet Explorer proxy on a Invoke-RestMethod command.

In Powershell V6, there is the -NoProxy option that indicates that the cmdlet will not use a proxy to reach the destination. This is to bypass the proxy configured in Internet Explorer and this is exactly what I want to do.

In Powershell V6, the code would be something like:

$Result = Invoke-RestMethod -Uri $url  -NoProxy

Any workaround in V5.1 ?

Thanks, Philippe

4

There are 4 best solutions below

2
On

As an alternative, but I think postanote has a great answer. How about dipping into .net? And going a level deeper than typical these days, so instead of using .net's HttpClient using WebRequest:

$request = [System.Net.WebRequest]::Create("https://www.example.org")
$request.Proxy = [System.Net.WebProxy]::new() #blank proxy
$response = $request.GetResponse()
$response

I haven't tested extensively if this bypasses a proxy (my corporate policy will make this tricky to test), but this c# question suggests it should do: How to remove proxy from WebRequest and leave DefaultWebProxy untouched

Of course you'd loose some of the pipelining magic in PowerShell, but you could wrap it easily enough, being careful of socket usage if your using this under heavy load.

3
On

Not natively, as these were some of the improvements, in PSCore web cmdlets and MS has stated that nothing will be backported. You can just shell out to PSCore from PS5x to use the cmdlet as is.

Something like this has be done for Invoke-WebRequest using this function:

Update-Proxy.ps1

As per this Q&A

Invoke-WebRequest Proxy Bypass

   $p = proxy
   $p.Override += "*.domain.com" 
   $p | proxy
   Invoke-WebRequest ...
   #you could return old override here.

So, it might work with Invoke-RestMethod, but I not in a location to test this.

1
On

So, here is the workaround I implemented very successfully:

The end of a web request is generally ignored by the site you query (please test, but it is very often true). So, I add something random at the end of my URL: a timestamp.

The proxy believes this is a new query each time, so there is no caching happening.

$timestamp = (Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s)
$url = "https://www.example.org/$timestamp"
$Result = Invoke-RestMethod -Uri $url

Works great in my case !!!

4
On

I know this is rather old, but as I like to have good solutions, too, I will post mine to this thread. I actually used this and it worked perfect (Also for Invoke-WebRequest):

    $Proxy=New-object System.Net.WebProxy
    $WebSession=new-object Microsoft.PowerShell.Commands.WebRequestSession
    $WebSession.Proxy=$Proxy
    $Antwort=Invoke-RestMethod -Method Post -Uri "https://thisismyrestsite" -Body $BodyJson -WebSession $WebSession

Maybe this helps someone else, as I did not find good solutions on the net so far. If someone needs special Proxy Settings, I believe he can also fill the $Proxy with the values, which might allow more settings than Invoke-RestMethod or Invoke-WebRequest. Edit: Please remember that this is only for 5.1. For Core, use the -NoProxy Switch!