IE通过注册表启用/禁用代理设置

时间:2014-02-05 04:12:47

标签: windows internet-explorer powershell registry

我需要在IE运行时启用/禁用IE代理设置。我有一个PowerShell脚本行来启用代理:

Set-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ProxyEnable -value 1


或者这个禁用:

Set-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ProxyEnable -value 0


上面的脚本工作,注册表项更新。但是,在我关闭所有打开的IE窗口并打开一个新窗口之前,IE不会获取该值。我需要已经打开/运行IE窗口来获取新设置。

有没有办法达到我想要的目的?

3 个答案:

答案 0 :(得分:5)

问题是IE不会重置代理设置,直到它

  1. 关闭,或
  2. 已刷新其配置。
  3. 以下是我用来实现这项工作的代码:

    function Refresh-System
    {
      $signature = @'
    [DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
    public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
    '@
    
    $INTERNET_OPTION_SETTINGS_CHANGED   = 39
    $INTERNET_OPTION_REFRESH            = 37
    $type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
    $a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
    $b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
    return $a -and $b
    }
    

答案 1 :(得分:4)

修改

下的代理值
[HKEY_USERS\<your SID>\Software\Microsoft\Windows\CurrentVersion\Internet Settings]

不需要重启即

答案 2 :(得分:-1)

我知道这是一个老问题,但是这里有一个简单的单行开关,可根据当前状态打开或关闭它:

set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'  -name ProxyEnable -value (-not ([bool](get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'  -name ProxyEnable).proxyenable))