Powershell v5.1 Invoke-RestMethod and bypass proxy
Asked Answered
B

4

7

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

Burgh answered 24/11, 2018 at 23:41 Comment(0)
O
7

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!

Orfurd answered 24/2, 2021 at 6:14 Comment(4)
This worked for a Invoke-WebRequest to an internal server that was being blocked by the proxy, but made no effect on Invoke-RestMethod at all...Demy
Invoke-Restmethod is exactly the one we are using tons of times with this. Maybe something else is going on, but this definitely works on Restmethod.Orfurd
yeah, I didn't actually paid attention to the PS version of this. Since I was using Core on Linux, I tried the -NoProxy mentioned in the question and that did the trick!Demy
Added the information about 5.1 / Core as an edit to my post.Orfurd
L
5

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.

Leathaleather answered 26/11, 2018 at 23:42 Comment(2)
Alex, just take a look at my above comment :-)Burgh
VSCode is becoming a great editor for powershell, I tend to mix and match with ISE. The nice thing about dipping into .net, is that the code above is still powershell it's just using the .net classes directly.Leathaleather
S
1

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.

Single answered 25/11, 2018 at 4:41 Comment(3)
I tried, no great results for the time being... I am seriously considering moving to PowerShell v6, but this will take a while. I'll post a follow-up answer once this in done but this will take some time... In my use-case, the proxy delays me of roughly 7 seconds :-(Burgh
Yep, the improved web cmdlets is the one the I use PSCore for and all things Azure Shell, but not much else. For me I have far too much investment in Windows PS, that will never work and never port over to v6. Especially any GUI WF/WPF stuff, since there are no GUI stuff in PSCore. That proxy delay sound like your org is using heavy web filtering (ingress/egress, may a WebSense stack) vs a transparent proxy, thus introducing that latency. Not much you can do about that other than lobbying them to ease restrictions on destinations you are trying to hit.Single
Thanks for both of your answers, I am in the process of drilling down :-) I also started looking at what Microsoft is doing with its Visual Sutdio Code [link]code.visualstudio.com This is really impressive and Powershell ISE is being part of this environment now. I am taking a look and will get back to you.Burgh
B
-4

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 !!!

Burgh answered 29/11, 2018 at 0:4 Comment(1)
Sorry to vote -1 but this has not really much to do with bypassing a proxy. It just "tells" your proxy to get a response, which might happen to be the first (non-cached) and if that's true the proxy will get a non-cached response from the target host. Maybe in your case you should look into the caching configuration of the proxy and/or the targeted host, and if applicable check the caching by your client (=the calling browser / application).Dodecasyllable

© 2022 - 2024 — McMap. All rights reserved.