PowerShell, Web Requests, and Proxies

前端 未结 6 1134
遇见更好的自我
遇见更好的自我 2020-12-12 17: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 contro

相关标签:
6条回答
  • 2020-12-12 17:45

    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
    
    0 讨论(0)
  • 2020-12-12 17:49

    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
    }
    
    0 讨论(0)
  • 2020-12-12 17:49

    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!

    0 讨论(0)
  • 2020-12-12 17:59

    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)
    

    ...

    0 讨论(0)
  • 2020-12-12 18:09
    $proxy = New-Object System.Net.WebProxy("http://yourProxy:8080")
    $proxy.useDefaultCredentials = $true
    $wc = new-object system.net.webclient
    $wc.proxy = $proxy
    $wc.downloadString($url)
    
    0 讨论(0)
  • 2020-12-12 18:09

    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.

    0 讨论(0)
提交回复
热议问题