6

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

Philippe
  • 103
  • 1
  • 1
  • 8

4 Answers4

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!

Mr-Fly
  • 71
  • 1
  • 3
  • 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... – João Ciocca Oct 15 '21 at 13:51
  • 1
    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. – Mr-Fly Oct 16 '21 at 15:07
  • 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! – João Ciocca Oct 17 '21 at 00:02
  • 1
    Added the information about 5.1 / Core as an edit to my post. – Mr-Fly Oct 18 '21 at 07:15
3

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.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • Alex, just take a look at my above comment :-) – Philippe Nov 27 '18 at 23:17
  • 1
    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. – Alex KeySmith Nov 28 '18 at 09:19
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.

postanote
  • 15,138
  • 2
  • 14
  • 25
  • 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 :-( – Philippe Nov 26 '18 at 22:03
  • 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. – postanote Nov 26 '18 at 23:24
  • 1
    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]https://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. – Philippe Nov 27 '18 at 23:16
-3

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

Philippe
  • 103
  • 1
  • 1
  • 8
  • 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). – Michael Sep 07 '21 at 10:36