Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. function Get-Downloader {
  2. param (
  3. [string]$url
  4. )
  5.  
  6. $downloader = new-object System.Net.WebClient
  7.  
  8. $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
  9. if ($defaultCreds -ne $null) {
  10. $downloader.Credentials = $defaultCreds
  11. }
  12.  
  13. $ignoreProxy = $env:chocolateyIgnoreProxy
  14. if ($ignoreProxy -ne $null -and $ignoreProxy -eq 'true') {
  15. Write-Debug "Explicitly bypassing proxy due to user environment variable"
  16. $downloader.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()
  17. } else {
  18. # check if a proxy is required
  19. $explicitProxy = $env:chocolateyProxyLocation
  20. $explicitProxyUser = $env:chocolateyProxyUser
  21. $explicitProxyPassword = $env:chocolateyProxyPassword
  22. if ($explicitProxy -ne $null -and $explicitProxy -ne '') {
  23. # explicit proxy
  24. $proxy = New-Object System.Net.WebProxy($explicitProxy, $true)
  25. if ($explicitProxyPassword -ne $null -and $explicitProxyPassword -ne '') {
  26. $passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force
  27. $proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd)
  28. }
  29.  
  30. Write-Debug "Using explicit proxy server '$explicitProxy'."
  31. $downloader.Proxy = $proxy
  32.  
  33. } elseif (!$downloader.Proxy.IsBypassed($url)) {
  34. # system proxy (pass through)
  35. $creds = $defaultCreds
  36. if ($creds -eq $null) {
  37. Write-Debug "Default credentials were null. Attempting backup method"
  38. $cred = get-credential
  39. $creds = $cred.GetNetworkCredential();
  40. }
  41.  
  42. $proxyaddress = $downloader.Proxy.GetProxy($url).Authority
  43. Write-Debug "Using system proxy server '$proxyaddress'."
  44. $proxy = New-Object System.Net.WebProxy($proxyaddress)
  45. $proxy.Credentials = $creds
  46. $downloader.Proxy = $proxy
  47. }
  48. }
  49.  
  50. return $downloader
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement