Remove System Proxy for Invoke-WebRequest and Invoke-RestMethod

We recently had a problem that accessing a function app through Invoke-RestMethod with using a private endpoint failed, as the System default proxy will be queried. In Powershell 7.x there is a switch for disabling the proxy (-NoProxy) but there is none for Powershell 5.1. So we were searching on the web but actually did not find a nice solution. All were talking about using a proxy, which can be easily done with the „-proxy“-Switch, but you cannot set this to „none“. So we finally found a solution, by first creating a new WebSession, setting the proxy to nothing and then handling all upcoming Invoke-RestMethod or Invoke-WebSession with a predefined WebSession. This will override the current user-default-proxy and will then just „go local“

So here it is:

$Proxy=New-object System.Net.WebProxy
$WebSession=new-object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Proxy=$Proxy
$Answer=Invoke-RestMethod -Method Post -Uri "https://my.restmethod.site" -WebSession $WebSession 
Dieser Beitrag wurde unter Allgemein, IT, Powershell abgelegt und mit , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image