【问题标题】:Using PowerShell to programmatically configure Internet Explorer proxy settings to work before it has been opened使用 PowerShell 以编程方式将 Internet Explorer 代理设置配置为在打开之前工作
【发布时间】:2018-06-18 09:51:46
【问题描述】:

许多 PowerShell cmdlet(例如 AWS cmdlet)参考 Internet Explorer 代理设置来查找要使用的代理和绕过列表。

我正在尝试配置代理服务器和绕过列表,以用作在新 AWS 实例上执行的用户数据脚本的一部分。

我已经尝试了多种方法来让它工作,但似乎无法让它可靠地工作。

首先让我们为代理服务器和旁路列表设置变量,注意这些地址根据实例所在的 VPC 不同:

$proxy = "security-elb-1111111111.us-east-2.elb.amazonaws.com:3128"
$bypassList = "169.254.169.254;octopus-769734587.us-east-2.elb.amazonaws.com;s3.dualstack.us-east-1.amazonaws.com"

其次,netsh:

netsh winhttp set proxy $proxy bypass-list=$bypassList

这很好用,但遗憾的是很多 cmdlet 都没有引用它。

这看起来也很有用:

& C:\windows\System32\bitsadmin.exe /Util /SetIEProxy LOCALSYSTEM Manual_proxy $proxy $bypassList

但这只会为帐户 LOCALSYSTEM、NETWORKSERVICE 或 LOCALSERVICE 设置代理服务器。我认为 userdata 脚本以管理员身份运行,所以这似乎不像最初出现的那么有用。

所以我尝试破解注册表,如下所示:

$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $reg -Name ProxyServer -Value $proxy
Set-ItemProperty -Path $reg -Name ProxyEnable -Value 1
Set-ItemProperty -Path $reg -Name ProxyOverride -Value $bypassList

这会打开代理并设置基本详细信息。但看起来好像缺少了一个重要的部分,因为这不起作用。然后我发现了

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections]

其中包含密钥 “DefaultConnectionSettings”和“SavedLegacySettings”。 这些似乎包含代理设置的十六进制版本。所以我创建了这个:

$proxyString = ""
    for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($proxy)).length); $i++) {
        if ($i % 2 -eq 0) {
            $byte = (([System.Text.Encoding]::Unicode.GetBytes($proxy))[$i])
            $convertedByte=%{[System.Convert]::ToString($byte,16)}
            $proxyString = $proxystring + $convertedByte  + ","
        }
    }
    $bypassString = ""
    for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($bypassList)).length); $i++) {
        if ($i % 2 -eq 0) {
            $byte = (([System.Text.Encoding]::Unicode.GetBytes($bypassList))[$i])
            $convertedByte=%{[System.Convert]::ToString($byte,16)}
            $bypassString = $bypassString + $convertedByte  + ","
        }
    }

这将 $proxy 和 $bypass 转换为十六进制字符串,我将它们组合成长字符串以在注册表中使用,如下所示:

$regString="46,00,00,00,00,00,00,00,0b,00,00,00,3c,00,00,00," + $proxystring + (%{[System.Convert]::ToString($bypassList.length,16)}) + ",00,00,00," + $bypassString +  "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"

这会放入一些(看似)任意行的十六进制(这可能是我的问题所在),然后是代理字符串,然后是绕过列表长度的十六进制指示符,然后是绕过列表,然后是由另一个看似任意的十六进制值系列。

它适用于某些网络上的实例,但不适用于其他网络。故障网络上的代理地址比工作网络短一个字符。

但奇怪的是,如果我登录机器,打开Internet Explorer 并再次关闭它,突然cmdlet 就可以成功使用代理了。

那么当我打开 Internet Explorer 时究竟发生了什么?必须有正在更改的注册表项有助于此过程?我尝试比较之前和之后的注册表项,但看不到任何内容。

我尝试的另一件事是以编程方式创建 PAC 文件来配置设置。我是这样做的:

'[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings]
"AutoConfigURL"="C:\\scripts\\proxyConfig.pac"'| out-file c:\proxyReg.reg
$proxy = "internal-sec-elb-937750220.us-east-1.elb.amazonaws.com:3128"
$bypassList = "169.254.169.254;internal-mgt-priv-adr-prod-man-lb-769734587.us-east-1.elb.amazonaws.com;" + "s3.dualstack.us-east-1.amazonaws.com"

$pacFileLocation = "c:\scripts\proxyConfig.pac"
if (!(test-path (split-path $pacFileLocation))) {New-Item -ItemType Directory -Path (Split-Path $pacFileLocation)}

$pacText = 'function FindProxyForURL(url, host) {
return "PROXY ' + $proxy + ';
DIRECT";
}
'
foreach ($localAddress in ($bypassList.Split(";"))) {
$pacText = $pacText + 'if (isPlainHostName(' + $localAddress+ '))
{
return "DIRECT";
}
'

}

$pacText = $pacText + "
if (isInNet(hostIP, '0.0.0.0', '255.0.0.0') ||
isInNet(hostIP, '10.0.0.0', '255.0.0.0') ||
isInNet(hostIP, '127.0.0.0', '255.0.0.0') ||
isInNet(hostIP, '169.254.0.0', '255.255.0.0') ||
isInNet(hostIP, '172.16.0.0', '255.240.0.0') ||
isInNet(hostIP, '192.0.2.0', '255.255.255.0') ||
isInNet(hostIP, '192.88.99.0', '255.255.255.0') ||
isInNet(hostIP, '192.168.0.0', '255.255.0.0') ||
isInNet(hostIP, '198.18.0.0', '255.254.0.0') ||
isInNet(hostIP, '224.0.0.0', '240.0.0.0') ||
isInNet(hostIP, '240.0.0.0', '240.0.0.0'))
{
return 'DIRECT';
}"


$pacText | out-file $pacFileLocation -Force

不过,这些设置似乎在 Internet Explorer 打开时才生效。

很遗憾,我无法打开 Internet Explorer,因为代理需要在用户登录计算机之前工作,而从脚本中运行 iexplore.exe 并不能做到这一点。

这一切似乎真的过于复杂了。我可以在两行内配置 linux 服务器中的代理。我肯定在某个地方错过了简单的解决方案。这是什么?

【问题讨论】:

    标签: powershell amazon-web-services internet-explorer proxy automation


    【解决方案1】:

    在为 Server Core 2016 设置代理时,我得到了与 Richard 相同的结果(略短)。太可怕了,这是必要的!

    function Set-Proxy($proxy, $bypassUrls){
        $proxyBytes = [system.Text.Encoding]::ASCII.GetBytes($proxy)
        $bypassBytes = [system.Text.Encoding]::ASCII.GetBytes($bypassUrls)
        $defaultConnectionSettings = [byte[]]@(@(70,0,0,0,0,0,0,0,11,0,0,0,$proxyBytes.Length,0,0,0)+$proxyBytes+@($bypassBytes.Length,0,0,0)+$bypassBytes+ @(1..36 | % {0}))
        $registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
        Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxy
        Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1
        Set-ItemProperty -Path "$registryPath\Connections" -Name DefaultConnectionSettings -Value $defaultConnectionSettings
        netsh winhttp set proxy $proxy bypass-list=$bypassUrls
    }
    Set-Proxy "someproxy:1234" "*.example.com;<local>"
    

    【讨论】:

    • 完全同意这应该更容易,不是吗。
    • 感谢您提供更简洁的解决方案!
    • 感谢您发布此消息!我一直在努力让 Windows 更新在安全网段中构建的系统上工作。这样就行了。
    • 谢谢!这正是我想要的。
    【解决方案2】:

    我想通了。

    缺少的关键部分是一个表示代理地址长度的十六进制字节。

    所以我将连接十六进制字符串的行改为:

    $regString="46,00,00,00,00,00,00,00,0b,00,00,00,"+(%{[System.Convert]::ToString($proxy.length,16)})+",00,00,00," + $proxystring + (%{[System.Convert]::ToString($bypassList.length,16)}) + ",00,00,00," + $bypassString +  "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"
    

    这很成功。

    所以对于腰带和大括号的方法: 网盘

    netsh winhttp set proxy $proxy bypass-list=$bypassList
    

    用于系统帐户的 Internet Explorer:

    foreach ($account in "LOCALSYSTEM","NETWORKSERVICE","LOCALSERVICE") {
        & C:\windows\System32\bitsadmin.exe /Util /SetIEProxy $account Manual_proxy $proxy $bypassList
    }
    

    当前帐户的 Internet Explorer:

    $proxyString = ""
    for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($proxy)).length); $i++) {
        if ($i % 2 -eq 0) {
            $byte = (([System.Text.Encoding]::Unicode.GetBytes($proxy))[$i])
            $convertedByte=%{[System.Convert]::ToString($byte,16)}
            $proxyString = $proxystring + $convertedByte  + ","
        }
    }
    $bypassString = ""
    for ($i = 0;$i -lt (([System.Text.Encoding]::Unicode.GetBytes($bypassList)).length); $i++) {
        if ($i % 2 -eq 0) {
            $byte = (([System.Text.Encoding]::Unicode.GetBytes($bypassList))[$i])
            $convertedByte=%{[System.Convert]::ToString($byte,16)}
            $bypassString = $bypassString + $convertedByte  + ","
        }
    }
    $regString="46,00,00,00,00,00,00,00,0b,00,00,00,"+(%{[System.Convert]::ToString($proxy.length,16)})+",00,00,00," + $proxystring + (%{[System.Convert]::ToString($bypassList.length,16)}) + ",00,00,00," + $bypassString +  "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"
    $regstringAsArray = ("0x"+$regString.replace(",",",0x")).Split(",")
    $reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    Set-ItemProperty -Path $reg -Name ProxyServer -Value $proxy
    Set-ItemProperty -Path $reg -Name ProxyEnable -Value 1
    Set-ItemProperty -Path $reg -Name ProxyOverride -Value $bypassList
    $reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
    Set-ItemProperty -Path $reg -Name DefaultConnectionSettings -Type Binary -Value $regstringAsArray
    Set-ItemProperty -Path $reg -Name SavedLegacySettings -Type Binary -Value $regstringAsArray
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2016-07-19
      • 2015-11-27
      • 1970-01-01
      • 2013-12-05
      相关资源
      最近更新 更多