36

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
Community
  • 1
  • 1
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

6 Answers6

24

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
}
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
  • When you say `auto-detected proxies`, do you mean automatic configuration scripts (as the option is called in Internet Explorer)? – Chris Apr 10 '14 at 17:33
  • I think so, yes (it's a long time since I wrote that :-)) – Paul Moore Apr 11 '14 at 18:10
  • Note that this doesn't seem to work on PS 2.0. I think there's a bug in how NTLM is handled by WebClient for older .net versions so... yeah make sure you have a recent version of PS for doing this. – Nacht Jun 07 '16 at 06:43
  • Oh and by the way THANK YOU i have been needing a solution to this problem for years. I thought I had tried every combination of API call but this is what finally got it working (plus being PS v4) – Nacht Jun 07 '16 at 06:55
  • This is old, but worked perfectly for me on a windows 10 machine using powershell 5.1. – aleith Apr 15 '19 at 01:29
13

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)

...

schoetbi
  • 12,009
  • 10
  • 54
  • 72
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
12

This is much later than the original question, but still a relevant answer for later versions of PowerShell. Starting in v3, we have two items that can address this:

Invoke-WebRequest - which replaces using system.net.webclient for nearly every scenario

$PSDefaultParameterValues - which can store details for parameters

How to use them together to solve the original problem of proxy settings controlled by a network policy(or script) and not having to modify ps scripts later on?

Invoke-WebRequest comes with -Proxy and -ProxyUseDefaultCredentials parameters.

We store our answers to these parameters in $PSDefaultParameterValues, like so: $PSDefaultParameterValues.Add('Invoke-WebRequest:Proxy','http://###.###.###.###:80') $PSDefaultParameterValues.Add('Invoke-WebRequest:ProxyUseDefaultCredentials',$true)

You can replace 'http://###.###.###.###:80' with $proxyAddr as you will. What scope you choose to store this in, is your choice. I put them into my $profile, so I never have to set these items in my scripts again.

Hope this helps someone!

Bewc
  • 431
  • 5
  • 15
11
$proxy = New-Object System.Net.WebProxy("http://yourProxy:8080")
$proxy.useDefaultCredentials = $true
$wc = new-object system.net.webclient
$wc.proxy = $proxy
$wc.downloadString($url)
shamp00
  • 11,106
  • 4
  • 38
  • 81
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • This wouldn't work for me. Instead of reading the webpage it reads the proxy prompt for credentials :( – Bluz Sep 11 '13 at 10:42
  • Try setting the your credentials via $proxy.credentials (and set $proxy.useDefaultCredentials to $false), YMMV. – Shay Levy Sep 11 '13 at 10:56
  • Nice one Shay, that was clever but unfortunately I still get the same page :( I think I'll just use the old way of doing it and copy paste the html I want into a text file and take it from there.It's a one off script anyway. Thanks for your help! – Bluz Sep 11 '13 at 13:22
1

I know this is really really old, but there's a right way and so few people seem to know it. The below will figure out what the proxy is for the URI that you're interested in and use it.

$uri = "http://www.google.com"
invoke-webrequest -ProxyUseDefaultCredentials -proxy (new-object System.Net.WebClient).Proxy.GetProxy($uri).AbsoluteUri $uri
T.Rojan
  • 111
  • 7
-1

Just update the URL with your own proxy address:port. It enables PowerShellGet to go past the proxy using your local credentials. If you don't have credential requirement, just click OK when prompted for your password. I renamed that box to "Close this window". You can also use other package managers like Chocolatey/Nuget through the proxy because of this script.

Bugs
  • 4,491
  • 9
  • 32
  • 41