Invoke-WebRequest GetSystemWebProxy()

后端 未结 3 1396
深忆病人
深忆病人 2021-02-09 03:09

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:

$pro         


        
相关标签:
3条回答
  • 2021-02-09 03:57

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-09 03:58

    Use can use this code :

    $dest = "http://www.google.fr"
    $proxyurl = ([System.Net.WebRequest]::GetSystemWebproxy()).GetProxy($dest)
    Invoke-WebRequest $dest -Proxy $proxyurl -ProxyUseDefaultCredentials
    
    0 讨论(0)
  • 2021-02-09 04:02

    The actual problem with your code above (even though Shay's is more elegant) is that you're trying to set a property on a variable before it exists. The SessionVariable "$WS" doesn't exist before you call Invoke-WebRequest but you're trying to set the .Proxy property above it.

    If it worked at one point, you had probably created an instance of $WS previously and therefore were able to work with the object during testing but on a fresh/dry run when the script was processing top-down, it didn't yet exist.

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