19

Windows中,我想使用批处理脚本禁用Internet 选项中的代理服务器设置。我可以使用什么命令来执行此操作?

如果不确定我指的是什么,请参阅

Internet Properties > Connections > LAN Settings >Proxy Server

谢谢

4

8 回答 8

20

它在注册表中,在[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]下

您可以使用REGBAT 中的命令,或准备几个.REG文件来自动进行更改。

例如,要禁用代理,请尝试

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
于 2013-08-26T08:07:02.457 回答
14

2021 年 5 月 25 日更新:现在从我的 GitHub 存储库获取图标和最新脚本:Windows_Proxy_Toggler。也可以在那里查看超级简单的安装说明。


这是一种使用简单 .vbs 脚本作为“小部件”类型桌面快捷方式的方法。第一次要运行脚本时,请单击您创建的 .vbs 文件。这将自动为您生成一个带有适当图标的桌面快捷方式。此后每次单击快捷方式都会切换代理设置,会弹出一个定时弹出框 1 秒,告诉您代理现在是打开还是关闭,并将快捷方式图标更改为打开或关闭符号以指示新代理状态。

文件:“C:\Users\YOUR_USERNAME\Proxy Settings\toggle_proxy_on_off.vbs”

'Toggle your Proxy on and off 
'Gabriel Staples - www.ElectricRCAircraftGuy.com
'Written: 21 June 2017 
'Updated: 25 June 2017 
'References: 
' 1) https://stackoverflow.com/a/27092872/4561887 
' 2) https://stackoverflow.com/a/26708451/4561887 
' Timed message boxes:
' - *****https://technet.microsoft.com/en-us/library/ee156593.aspx
' - https://stackoverflow.com/questions/14105157/automatically-close-msgbox-in-vbscript
' Debug output:
' - ex: Wscript.Echo "here is your message"

Option Explicit 

'Variables & Constants:
Dim ProxySettings_path, VbsScript_filename 
ProxySettings_path = "C:\Users\Gabriel\Proxy Settings"
VbsScript_filename = "toggle_proxy_on_off.vbs"
Const MESSAGE_BOX_TIMEOUT = 1 'sec; change this value to set how long the message box displays when you toggle the proxy setting 
Const PROXY_OFF = 0

Dim WSHShell, proxyEnableVal, username 
Set WSHShell = WScript.CreateObject("WScript.Shell")
'get the username string for use in path names, since trying to use the "%USERNAME%" variable directly in path names throws an error
username = WSHShell.ExpandEnvironmentStrings("%USERNAME%") 

'Determine current proxy setting and toggle to opposite setting
proxyEnableVal = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If proxyEnableVal = PROXY_OFF Then 
  TurnProxyOn
Else
  TurnProxyOff
End If

'Subroutine to Toggle Proxy Setting to ON
Sub TurnProxyOn 
  'turn proxy on via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("on")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now ON", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub TurnProxyOff 
  'turn proxy off via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("off")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now OFF", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to create or update a shortcut on the desktop 
Sub CreateOrUpdateDesktopShortcut(onOrOff)
  'create a shortcut 
  Dim shortcut, iconStr
  Set shortcut = WSHShell.CreateShortcut("C:\Users\" + username + "\Desktop\Proxy On-Off.lnk")
  'Set the target path (target file) to run when the shortcut is clicked 
  shortcut.TargetPath = ProxySettings_path + "\" + VbsScript_filename
  'Set the working directory. This is necessary in case you ever make this shortcut call a batch (.bat) file, for instance, which in turn calls a .vbs script. In order to know where the .vbs script file/command is located, the shortcut must be operating in the working directory where the .vbs scripts are located. Otherwise, calls to the .vbs scripts from a .bat file this shortcut points to, for instance, won't work since their directories are not in the Windows %PATH% variable, and you'll get an error which states: "'name_of_vbs_script_file' is not recognized as an internal or external command, operable program or batch file."
  shortcut.WorkingDirectory = ProxySettings_path 
  'Set the icon to associate with this shortcut 
  If onOrOff = "on" Then
    iconStr = "on.ico"
  ElseIf onOrOff = "off" Then
    iconStr = "off.ico"
  End If 
  shortcut.IconLocation = ProxySettings_path + "\Icons\" + iconStr
  'Save the shortcut 
  shortcut.Save
End Sub 

指示:

  1. 创建一个名为“C:\Users\YOUR_USERNAME\Proxy Settings”的文件夹
  2. 如上所示,在该文件夹中创建“toggle_proxy_on_off.vbs”文件。
  3. 在此处创建一个“图标”文件夹:“C:\Users\YOUR_USERNAME\Proxy Settings\Icons”
  4. 下载以下两个 .png 图像(更新:死链接——现在从我的 GitHub 存储库获取图标和脚本):
  5. 例如,使用http://icoconvert.com/将这些图像转换为图标(.ico 文件) 。选择文件(从上面选择一个.png)-> 上传--> 选择“ICO for Windows 7, Windows 8, Vista and XP”格式--> 点击“转换 ICO”--> 点击“下载您的图标(s )"
    • 将 ON 图标另存为“C:\Users\YOUR_USERNAME\Proxy Settings\Icons\on.ico”
    • 将 OFF 图标另存为“C:\Users\YOUR_USERNAME\Proxy Settings\Icons\off.ico”
  6. 现在,双击“C:\Users\Gabriel\Proxy Settings\toggle_proxy_on_off.vbs”文件运行它。它将自动在您的桌面上创建一个“代理开关”快捷方式文件,并带有相应的图标来指示代理是打开还是关闭。

从此时起,只需直接单击“代理开关”桌面快捷方式即可打开和关闭代理。

这是代理关闭时的样子:

在此处输入图像描述

这是代理打开时的样子:

在此处输入图像描述

这是一个 1 秒弹出窗口的示例,当您单击快捷方式图标以打开/关闭代理时,该窗口就会出现。

在此处输入图像描述

参考:

  1. 禁用 Internet 选项代理服务器的批处理文件<--教我如何使用 .vbs 脚本来打开和关闭代理
  2. 用于打开和关闭代理的 Windows 桌面小部件<-- 教会了我如何通过创建 Windows 快捷方式并在每次单击它时更改其图标来使 .vbs 脚本像小部件一样工作的巧妙技术
  3. 定时消息框:

去做:

有人可以帮我弄清楚如何通过使其每次都更改图标名称来进一步增强这一步骤吗?--即:不要在快捷方式上说“代理开关”,而是说“代理已打开”或“代理已关闭”,根据其当前状态。我不知道如何更进一步,我现在已经投入了足够的时间......

于 2017-06-26T03:15:39.707 回答
11

使用 .vbs打开关闭代理

如果您想打开和关闭代理,此 .vbs MS 脚本确定当前代理设置并切换到相反设置非常方便

Option Explicit 
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")

'Determine current proxy setting and toggle to oppisite setting
strSetting = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If strSetting = 1 Then 
NoProxy
Else Proxy
End If

'Subroutine to Toggle Proxy Setting to ON
Sub Proxy 
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy 
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub
于 2014-11-23T18:50:45.020 回答
9

Internet explorer and Google chrome both shares same proxy settings. So if we change setting in internet explorer, then it also effects in Google chrome. We can change proxy setting from CMD (command line prompt).

Disable proxy setting:

@ECHO OFF

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

Enable proxy setting:

@ECHO OFF

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d address:portNumber /f

address: New proxy address
portNumber: Port Number

Save the commands in a batch file and execute it. It will disable/enable the proxy setting for browser.

I found this answer at: http://langbasics.blogspot.in/2012/11/disable-or-enable-proxy-for-internet.html

于 2015-01-28T11:58:55.663 回答
1

感谢@Gabriel Staples 的回答 https://stackoverflow.com/a/44752679/6070417

先按照步骤做,

但有两点需要注意:

1,就像@afxentios 在评论中说的:

需要进行更正。在 username = >WSHShell.ExpandEnvironmentStrings("%USERNAME%") 行下添加行:ProxySettings_path = "C:\Users\" + username + >"\Proxy Settings" 并删除硬编码路径。

修复步骤

a) 将此行放到toggle_proxy_on_off.vbs的第 26 行下:

ProxySettings_path = "C:\Users\" + username + "\Proxy Settings"

b) 删除第 18 行ProxySettings_path = "C:\Users\Gabriel\Proxy Settings"

2、你会看到这个脚本确实更新了注册表,但是只有你打开/关闭一次 IE 才会生效。所以我在这里找到了答案:https ://stackoverflow.com/a/39079005/6070417

修复步骤

a) 复制脚本并保存到 Refresh-System.ps1

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

b) 将文件 Refresh-System.ps1 放入“C:\Users\YOUR_USERNAME\Proxy Settings”

c)将此行添加到“End If”下的toggle_proxy_on_off.vbs(大约第35行)

WSHShell.run("powershell -windowstyle hidden -file """ + ProxySettings_path + "\Refresh-System.ps1""")

该脚本将在没有 IE 的情况下运行。

.

但是现在当vbs脚本调用powershell脚本时,powershell窗口会出现一小会儿。

谁知道如何设置powershell窗口从不显示?请添加评论。

于 2019-09-19T05:29:29.637 回答
1

禁用代理

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet 设置" /v ProxyEnable /t REG_DWORD /d 0 /f

启用代理

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet 设置" ^ /v ProxyEnable /t REG_DWORD /d 1 /f

设置代理

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^ /v ProxyServer /t REG_SZ /d ProxyServerIP:Port /f

于 2019-01-31T06:29:29.950 回答
0

试试这个来禁用代理。PA 接受的答案会更改注册表中的值,但要使更改生效,如注释中所述,必须重新启动 IE。

将下一个内容保存为 cmd- 或 bat-file 并运行它:

REM turn off proxy server setting
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

REM restart IE for changes to take effect
start /w /b iexplore.exe
taskkill /f /im iexplore.exe

如果您想将代理切换为“开启”(或 1),您可以执行以下操作:

REM turn on proxy server setting
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f

REM you would likely also want to specify your proxy server and port 
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d ADDRESS:PORT /f

REM restart IE for changes to take effect
start /w /b iexplore.exe
taskkill /f /im iexplore.exe

这在 Windows 7 和 Windows 10 上进行了检查。在此处此处查看更多信息。

于 2021-07-09T17:43:16.080 回答
0

我编写了一个带有“启用/禁用代理”选项的脚本,并以管理员身份启动。你只需要将它复制到一个file.bat:

@echo off
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B
:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------


@echo off
Setlocal EnableDelayedExpansion


:MAIN_M
ECHO
ECHO 

ECHO 0. QUIT
ECHO 1. ENABLE PROXY 10.10.10.10:8181
ECHO 2. DISABLE PROXY


set /p choice=CHOISE....    


if ´%choice%´==´0´ goto EXIT_M
if ´%choice%´==´1´ goto ENABLE_PROXY
if ´%choice%´==´2´ goto DISABLE_PROXY

:EXIT_M
exit

:DISABLE_PROXY
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
goto MAIN_M

:ENABLE_PROXY
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d 10.10.10.10:8181 /f
goto MAIN_M   
于 2020-03-10T19:00:36.773 回答