Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell, Web Requests, and Proxies

When making a simple web request is there a way to tell the PowerShell environment to just use your Internet Explorer's proxy settings?

My proxy settings are controlled by a network policy(or script) and I don't want to have to modify ps scripts later on if I don't have to.

UPDATE: Great info from the participants. The final script template that I'll use for this will look something like the following:

$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer $proxy = new-object System.Net.WebProxy $proxy.Address = $proxyAddr $proxy.useDefaultCredentials = $true  $url = "http://stackoverflow.com" $wc = new-object system.net.WebClient $wc.proxy = $proxy $webpage = $wc.DownloadData($url) $str = [System.Text.Encoding]::ASCII.GetString($webpage) Write-Host $str 
like image 445
BuddyJoe Avatar asked Feb 20 '09 22:02

BuddyJoe


People also ask

Does invoke-WebRequest use proxy?

Beginning in PowerShell 7.0, Invoke-WebRequest supports proxy configuration defined by environment variables.

Does PowerShell use proxy settings?

The fact is that Powershell (or rather, the . NET class System. Net. WebClient, which these cmdlets used to access external resources over HTTP/HTTPS) does not use the proxy server settings specified in the user settings.

Does test Netconnection use proxy?

Test-NetConnect should use your proxy server if its correctly configured without having to specifically specify it. What error are you getting from the command? If this is like other powershell cmdlets, you can configure the System.


2 Answers

Untested:

$user = $env:username $webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer $pwd = Read-Host "Password?" -assecurestring  $proxy = new-object System.Net.WebProxy $proxy.Address = $webproxy $account = new-object System.Net.NetworkCredential($user,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)), "") $proxy.credentials = $account  $url = "http://stackoverflow.com" $wc = new-object system.net.WebClient $wc.proxy = $proxy $webpage = $wc.DownloadData($url) $string = [System.Text.Encoding]::ASCII.GetString($webpage) 

...

like image 35
Mitch Wheat Avatar answered Sep 25 '22 19:09

Mitch Wheat


Somewhat better is the following, which handles auto-detected proxies as well:

$proxy = [System.Net.WebRequest]::GetSystemWebProxy() $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials  $wc = new-object system.net.WebClient $wc.proxy = $proxy $webpage = $wc.DownloadData($url) 

(edit) Further to the above, this definition appears to work fine for me, too:

function Get-Webclient {     $wc = New-Object Net.WebClient     $wc.UseDefaultCredentials = $true     $wc.Proxy.Credentials = $wc.Credentials     $wc } 
like image 192
Paul Moore Avatar answered Sep 23 '22 19:09

Paul Moore