Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-WebRequest GetSystemWebProxy()

Under PowerShell 2.0 I know that you can set the proxy you would like to use without knowing the exact proxy settings by doing something like the following:

$proxy = [System.Net.WebRequest]::GetSystemWebproxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

Now, my question is if I don't know the proxy settings can I use the above and combine it with a PowerShell 3.0 Invoke-WebRequest. Here's what I was hoping to be able to do:

$proxy = [System.Net.WebRequest]::GetSystemWebproxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$WS.Proxy = $proxy

$login = Invoke-WebRequest https://website.com/login_form.html -SessionVariable WS

However, when I attempt to do this I get an error, (apparently from my company proxy) indicating that my credentials cannot be verified. I'm hoping that this will ultimately work, but perhaps I'm just making a simple mistake.

like image 844
Chris Avatar asked Mar 26 '13 19:03

Chris


2 Answers

Maybe this can help, I keep it in my profile. It is using the new the $PSDefaultParameterValues preference variable to set the default proxy values for the new web cmdlets. The code detects if I'm in my office environment and set the settings accordingly. This saves me specifying the settings each time I use those commands.

if(Test-Connection myCorpProxy -Count 1 -Quiet)
{
    $global:PSDefaultParameterValues = @{
        'Invoke-RestMethod:Proxy'='http://myCorpProxy:8080'
        'Invoke-WebRequest:Proxy'='http://myCorpProxy:8080'
        '*:ProxyUseDefaultCredentials'=$true
    }
}
like image 105
Shay Levy Avatar answered Oct 20 '22 04:10

Shay Levy


Use can use this code :

$dest = "http://www.google.fr"
$proxyurl = ([System.Net.WebRequest]::GetSystemWebproxy()).GetProxy($dest)
Invoke-WebRequest $dest -Proxy $proxyurl -ProxyUseDefaultCredentials
like image 14
Philippe Pecqueur Avatar answered Oct 20 '22 03:10

Philippe Pecqueur