15 May 2020

PowerShell behind a proxy - Annoying combination

Here is a list of all the methods I found to get PowerShell to work behind an authenticating proxy:

No 1:
$wc = New-Object System.Net.WebClient                        
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials                        
$wc.DownloadString('http://microsoft.com')
No 2:
$proxy = New-Object System.Net.WebClient                        
$Proxy.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials                        
$webclient = New-Object System.Net.WebClient
No 3:
$creds = Get-Credential #Prompts for credentials                        
$webclient.Proxy.Credentials = $creds
No 4:
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials            
[Net.ServicePointManager]::SecurityProtocol = "tls12"
No 5:
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://proxy.domain.lan:8080')                        
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials                        
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
No 6:
$browser = New-Object System.Net.WebClient            
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
No 7:

Check current settings for the PowerShell session:
netsh winhttp show proxy

Output current WinHTTP proxy settings:
Direct access (no proxy server)

Set the proxy server:
netsh winhttp set proxy proxy.domain.lan:8080
Set the proxy server with bypass list:
netsh winhttp set proxy proxy-server="http=proxy.domain.lan;https=proxy.domain.lan:8080" bypass-list="*.domain.lan;10.*"
Check current WinHTTP proxy settings: Proxy Server(s) : proxy.domain.lan:8080 Bypass-List : (none)

Reset to no proxy server settings:
netsh winhttp reset proxy

Output reset WinHTTP proxy settings:
Direct access (no proxy server)

No comments:

Post a Comment