windows 禁用 Internet 选项代理服务器的批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18439373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch File to disable internet options proxy server
提问by DextrousDave
In Windows, I want to disable the Proxy Server setting in Internet Optionsby using a batch Script. What command can I use to do this?
在Windows 中,我想使用批处理脚本禁用Internet 选项中的代理服务器设置。我可以使用什么命令来执行此操作?
If unsure what I am referring to, see
如果不确定我指的是什么,请参阅
Internet Properties > Connections > LAN Settings >Proxy Server
Thank you
谢谢
回答by PA.
It's in the registry, under [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
它在注册表中,在[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] 下
you can either use the REG
command in your BAT, or prepare a couple of .REG
files, to automate the changes.
您可以REG
在 BAT 中使用该命令,也可以准备几个.REG
文件来自动进行更改。
for example, to disable Proxy, try
例如,要禁用代理,请尝试
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
回答by Gabriel Staples
Here is a way using a simple .vbs script as a "widget" type desktop shortcut. The first time you want to run the script, click the .vbs file you create. This will automatically generate a desktop shortcut for you with an appropriate icon. Each time you click the shortcut thereafter it toggles the proxy setting, brings up a timed popup box for 1 sec to tell you whether the proxy is ONor OFFnow, and changes the shortcut icon to an ON or OFF symbol to indicate the new proxy state.
这是一种使用简单的 .vbs 脚本作为“小部件”类型桌面快捷方式的方法。第一次要运行脚本时,单击您创建的 .vbs 文件。这将自动为您生成一个带有适当图标的桌面快捷方式。此后每次单击快捷方式都会切换代理设置,弹出一个 1 秒的定时弹出框,告诉您代理现在是打开还是关闭,并将快捷方式图标更改为打开或关闭符号以指示新代理状态。
File: "C:\Users\YOUR_USERNAME\Proxy Settings\toggle_proxy_on_off.vbs"
文件:“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
Instructions:
指示:
- Create a folder called "C:\Users\YOUR_USERNAME\Proxy Settings"
- Create the "toggle_proxy_on_off.vbs" file, as shown above, in that folder.
- Create an "Icons" folder here: "C:\Users\YOUR_USERNAME\Proxy Settings\Icons"
- Download the following two .png images:
- ON icon image: http://s30.postimg.org/sgoerz0od/image.png
- OFF icon image: http://s13.postimg.org/9zha38zkj/off.png
- Convert those images to icons (.ico files) using http://icoconvert.com/, for example. Choose File (choose a .png from above) --> Upload --> choose "ICO for Windows 7, Windows 8, Vista and XP" format --> click "Convert ICO" --> click "Download your icon(s)"
- Save the ON icon as "C:\Users\YOUR_USERNAME\Proxy Settings\Icons\on.ico"
- Save the OFF icon as "C:\Users\YOUR_USERNAME\Proxy Settings\Icons\off.ico"
- Now, double-click the "C:\Users\Gabriel\Proxy Settings\toggle_proxy_on_off.vbs" file to run it. It will automatically create a "Proxy On-Off" shortcut file on your desktop, with the appropriate icon to indicate whether the Proxy is ON or OFF.
- 创建一个名为“C:\Users\YOUR_USERNAME\Proxy Settings”的文件夹
- 在该文件夹中创建“toggle_proxy_on_off.vbs”文件,如上所示。
- 在此处创建一个“图标”文件夹:“C:\Users\YOUR_USERNAME\Proxy Settings\Icons”
- 下载以下两个 .png 图像:
- 例如,使用http://icoconvert.com/将这些图像转换为图标(.ico 文件)。选择文件(从上面选择一个 .png)--> 上传--> 选择“ICO for Windows 7、Windows 8、Vista 和 XP”格式--> 单击“转换 ICO”--> 单击“下载您的图标” )”
- 将 ON 图标保存为“C:\Users\YOUR_USERNAME\Proxy Settings\Icons\on.ico”
- 将关闭图标保存为“C:\Users\YOUR_USERNAME\Proxy Settings\Icons\off.ico”
- 现在,双击“C:\Users\Gabriel\Proxy Settings\toggle_proxy_on_off.vbs”文件来运行它。它将自动在您的桌面上创建一个“代理开-关”快捷方式文件,并带有适当的图标来指示代理是打开还是关闭。
From this point on, just click the "Proxy On-Off" desktop shortcut directly to toggle the Proxy on and off.
从现在开始,只需直接单击“代理开-关”桌面快捷方式即可打开和关闭代理。
Here's what it looks like when the Proxy is OFF:
这是代理关闭时的样子:
Here's what it looks like when the Proxy is ON:
这是代理打开时的样子:
Here's an example of the 1-second popup window that comes up whenever you click the shortcut icon to toggle the Proxy on/off.
这是每当您单击快捷方式图标以打开/关闭代理时出现的 1 秒弹出窗口示例。
References:
参考:
- https://stackoverflow.com/a/27092872/4561887<-- taught me how to use a .vbs script to toggle the Proxy on and off
- https://stackoverflow.com/a/26708451/4561887<-- taught me the ingenious technique on how to make a .vbs script act like a widget by creating a Windows shortcut and changing its icon each time you click on it
- Timed message boxes:
- https://stackoverflow.com/a/27092872/4561887<--教我如何使用 .vbs 脚本打开和关闭代理
- https://stackoverflow.com/a/26708451/4561887<-- 通过创建 Windows 快捷方式并在每次单击时更改其图标,教我如何使 .vbs 脚本像小部件一样工作的巧妙技术
- 定时消息框:
Todo:
去做:
Can someone please help me figure out how to enhance this one step further by making it change the icon name each time too?--ie: instead of saying "Proxy On-Off" on the shortcut, have it say "Proxy is ON" or "Proxy is OFF", according to its current state.I'm not sure how to take it that one step further, and I've put enough time into it for now...
有人可以帮助我弄清楚如何通过每次更改图标名称来进一步增强这一步骤吗?--即:不要在快捷方式上说“代理开-关”,而是说“代理已打开”或“代理已关闭”,根据其当前状态。我不知道如何更进一步,我现在已经投入了足够的时间......
回答by BOB
Turn proxy onand offwith a .vbs
打开代理上,并关闭具有.VBS
This .vbs MS Script Determine current proxy setting and toggle to oppisite setting very handy if you want to turn proxy on and off
这个 .vbs MS Script 确定当前代理设置并切换到 oppisite 设置非常方便,如果你想打开和关闭代理
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
回答by Vikky Jain
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).
Internet Explorer 和 Google chrome 都共享相同的代理设置。因此,如果我们更改 Internet Explorer 中的设置,那么它也会影响谷歌浏览器。我们可以从 CMD(命令行提示符)更改代理设置。
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 addressportNumber
: Port Number
address
: 新代理地址portNumber
: 端口号
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
我在以下位置找到了这个答案:http: //langbasics.blogspot.in/2012/11/disable-or-enable-proxy-for-internet.html
回答by EKNATH KULKARNI
Disable the proxy
禁用代理
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
Enable the proxy
启用代理
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^ /v ProxyEnable /t REG_DWORD /d 1 /f
reg 添加“HKCU\Software\Microsoft\Windows\CurrentVersion\Internet 设置”^ /v ProxyEnable /t REG_DWORD /d 1 /f
Set the proxy
设置代理
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^ /v ProxyServer /t REG_SZ /d ProxyServerIP:Port /f
reg 添加“HKCU\Software\Microsoft\Windows\CurrentVersion\Internet 设置”^ /v ProxyServer /t REG_SZ /d ProxyServerIP:Port /f
回答by fisheep
Thanks the answer from @Gabriel Staples https://stackoverflow.com/a/44752679/6070417
感谢@Gabriel Staples 的回答 https://stackoverflow.com/a/44752679/6070417
Just do as the steps first,
先按步骤做,
but there is two things should to be watched:
但有两点需要注意:
1, Like @afxentios said in comment:
1、就像@afxentios 在评论中所说:
A correction is needed. Add the line: ProxySettings_path = "C:\Users\" + username + >"\Proxy Settings" under the line username = >WSHShell.ExpandEnvironmentStrings("%USERNAME%") and remove the hard coded path.
需要更正。在 username = >WSHShell.ExpandEnvironmentStrings("%USERNAME%") 行下添加行:ProxySettings_path = "C:\Users\" + username + >"\Proxy Settings" 并删除硬编码路径。
Fix Steps
修复步骤
a) Put this line to toggle_proxy_on_off.vbsunder line 26:
a) 将此行放在第 26行下的toggle_proxy_on_off.vbs 中:
ProxySettings_path = "C:\Users\" + username + "\Proxy Settings"
b) Remove line 18 ProxySettings_path = "C:\Users\Gabriel\Proxy Settings".
b) 删除第 18 行ProxySettings_path = "C:\Users\Gabriel\Proxy Settings"。
2, You will see the script indeed update the registry, but it won't work until you open/close IE once. So I found the answer here: https://stackoverflow.com/a/39079005/6070417
2、您会看到脚本确实更新了注册表,但是直到您打开/关闭IE一次它才会起作用。所以我在这里找到了答案:https: //stackoverflow.com/a/39079005/6070417
Fix Steps
修复步骤
a) Copy the script blow and save to Refresh-System.ps1
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) Put file Refresh-System.ps1 to "C:\Users\YOUR_USERNAME\Proxy Settings"
b) 将文件 Refresh-System.ps1 放入“C:\Users\YOUR_USERNAME\Proxy Settings”
c) Add this line to toggle_proxy_on_off.vbsunder "End If"(about line 35)
c) 将此行添加到“End If”下的toggle_proxy_on_off.vbs(约第 35 行)
WSHShell.run("powershell -windowstyle hidden -file """ + ProxySettings_path + "\Refresh-System.ps1""")
The script will work without IE.
该脚本将在没有 IE 的情况下工作。
.
.
But now when vbs script call powershell script, the powershell window will appear a short moment.
但是现在当 vbs 脚本调用 powershell 脚本时,powershell 窗口会出现一小会儿。
Who knows how to set the powershell window never show? please add a comment.
谁知道怎么设置powershell窗口永不显示?请添加评论。
回答by psycho
I wrote a script with "enable/disable proxy" options and that starts as Administrator. you just need to copy it to a file.bat:
我编写了一个带有“启用/禁用代理”选项的脚本,并以管理员身份启动。你只需要将它复制到一个 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