调用 WebRequest GetSystemWebProxy() [英] Invoke-WebRequest GetSystemWebProxy()

查看:77
本文介绍了调用 WebRequest GetSystemWebProxy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 2.0 下,我知道您可以通过执行以下操作来设置您想要使用的代理,而无需知道确切的代理设置:

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

现在,我的问题是,如果我不知道代理设置,我可以使用上述内容并将其与 PowerShell 3.0 Invoke-WebRequest 结合使用.这是我希望能够做到的:

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.

推荐答案

也许这会有所帮助,我将它保存在我的个人资料中.它使用新的 $PSDefaultParameterValues 首选项变量来设置新 Web cmdlet 的默认代理值.该代码检测我是否在我的办公环境中并相应地设置设置.这让我每次使用这些命令时都无需指定设置.

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
    }
}

这篇关于调用 WebRequest GetSystemWebProxy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆