Invoke-WebRequest GetSystemWebProxy ()

In PowerShell 2.0, I know that you can install the proxy server that you want to use without knowing the exact settings of the proxy server by doing something like the following:

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

Now, my question is, if I do not know the proxy server settings I can use above and combine it with PowerShell 3.0 Invoke-WebRequest. Here is what I was hoping 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 try to do this, I get an error (apparently from my proxy server) indicating that my credentials cannot be verified. I hope this works out eventually, but maybe I'm just making a simple mistake.

+5
source share
3 answers

, , . $PSDefaultParameterValues - -. , . , .

if(Test-Connection myCorpProxy -Count 1 -Quiet)
{
    $global:PSDefaultParameterValues = @{
        'Invoke-RestMethod:Proxy'='http://myCorpProxy:8080'
        'Invoke-WebRequest:Proxy'='http://myCorpProxy:8080'
        '*:ProxyUseDefaultCredentials'=$true
    }
}
+10

:

$dest = "http://www.google.fr"
$proxyurl = ([System.Net.WebRequest]::GetSystemWebproxy()).GetProxy($dest)
Invoke-WebRequest $dest -Proxy $proxyurl -ProxyUseDefaultCredentials
+3

( Shay ) , . SessionVariable "$ WS" Invoke-WebRequest, .Proxy.

If this worked at some point, you probably created an instance of $ WS earlier and, therefore, were able to work with the object during testing, but in the "fresh / dry" mode, when the script processed from top to bottom, it did not exist.

0
source

All Articles