powershell中的代理配置

rai*_*tin 5 powershell proxy

我正在尝试在代理后面的 Windows 上安装 Chocolatey:

@powershell -ExecutionPolicy unrestricted
Run Code Online (Sandbox Code Playgroud)

在电源外壳中我正在执行

$wc=new-object net.webclient;
$wc.Proxy=new-object system.net.WebProxy('<myproxy-ip>:8012',$true);
$wc.Proxy.Credentials = new-object system.net.NetworkCredential('<myusername>','<mypass>');
iex ($wc.DownloadString('https://chocolatey.org/install.ps1'));
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."
At line:1 char:1
+ iex ($wc.DownloadString('https://chocolatey.org/install.ps1'));
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
Run Code Online (Sandbox Code Playgroud)

我使用与启动 firefox/iexplorer 时必须输入的用户名/密码相同的用户名/密码(参见图片)。没有为代理配置默认用户名/密码,因为我必须始终输入它们。

在此处输入图片说明


更多详细信息(在私有窗口上的 firefox 中使用 Inspect Element)

响应头

Cache-Control: no-cache
Connection: close
Content-Length: 813
Content-Type: text/html; charset=utf-8
Pragma: no-cache
Proxy-Authenticate: BASIC realm="PROXY_INTERNET"
Proxy-Connection: close
Set-Cookie: BCSI-CS-dfaeac52a135c7c0=2; Path=/
Run Code Online (Sandbox Code Playgroud)

Ƭᴇc*_*007 0

无法测试它(没有类似的代理可供我使用),所以我实际上不知道这是否有效,但您可以尝试以下方法:

$wc = new-object net.webclient;
$proxyUri = new-object system.uri("http://<myproxy-ip>:8012");
$wc.Proxy = new-object system.net.WebProxy($proxyUri, $true);
$cachedCredentials = new-object system.net.CredentialCache;
$netCredential = new-object system.net.NetworkCredential("<myusername>", "<mypass>");

$cachedCredentials.Add($proxyUri, "Basic", $netCredential);

$wc.Proxy.Credentials = $cachedCredentials.GetCredential($proxyUri, "Basic");

iex ($wc.DownloadString("https://chocolatey.org/install.ps1"));
Run Code Online (Sandbox Code Playgroud)

目的是使用 CredentialCache 对象强制凭据使用“基本”身份验证模式。