views:

1029

answers:

2

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
+3  A: 

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)

...

Mitch Wheat
+1 and answer. Nice one.
tyndall
+3  A: 

$proxy = New-Object System.Net.WebProxy("http://yourProxy:8080")

$proxy.useDefaultCredentials = $true

$wc = new-object system.net.webclient

$wc.proxy = $proxy

$wc.downloadString($url)

Shay Levy