Powershell v5.1 Invoke-RestMethod and bypass proxy

Philippe picture Philippe · Nov 25, 2018 · Viewed 11.3k times · Source

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

Answer

Mr-Fly picture Mr-Fly · Feb 24, 2021

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!