2009-05-26

VB Script to Switch Proxy Server

I need to switch proxy server frequently. For Firefox, there is a handy add-on called FoxyProxy. However, for Internet Explorer, I have resorted to write a VB script called switch_proxy.vbs and put it on a convenient point (either at Start menu or on Desktop)

After clicking it, the following selection menu will pop up:

For instance, if option 1 is selected, then the acknowledgment dialogue will display to confirm the new setting:

On the other hand, if option 0 to disable the proxy setting, the following dialogue will be shown:

The source code is pasted as follows:



Rem Change the following entries to suit you name
ProxyName1 = "Proxy Server 1"
ProxyServer1 = "proxy.server1.com"
ProxyPort1 = "8080"
ProxyName2 = "Proxy Server 2"
ProxyServer2 = "proxy.server2.com"
ProxyPort2 = "8080"


Const HKEY_CURRENT_USER = &H80000001
strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"

strValueName = "ProxyEnable"
objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
IntProxy = strValue

strValueName = "ProxyServer"
objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
StrProxy = strValue

dim usr_input
usr_input=InputBox("Current ProxyEnable = " & IntProxy & chr(13) & chr(13) & _
"Current Proxy Server = " & StrProxy & chr(13) & chr(13) & _
"Enter New Choice :" & chr(13) & chr(13) & _
"0 : Disable Proxy" & chr(13) & _
"1 : " & ProxyName1 & chr(13) & _
"2 : " & ProxyName2 , _
"Proxy Management")

if usr_input = "" then WScript.Quit

Select Case usr_input
Case "0"
strValueName = "ProxyEnable"
dwValue = 0
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

Case "1"
strValueName = "ProxyEnable"
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

strValueName = "ProxyServer"
strValue = ProxyServer1 & ":" & ProxyPort1
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

Case "2"
strValueName = "ProxyEnable"
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

strValueName = "ProxyServer"
strValue = ProxyServer2 & ":" & ProxyPort2
objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
End Select

strValueName = "ProxyEnable"
objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
IntProxy = strValue

strValueName = "ProxyServer"
objRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue
StrProxy = strValue

MsgBox "ProxyEnable = " & IntProxy & chr(13) & chr(13) & "Proxy Server = " & StrProxy, ,"New Proxy Settings"



You can change the first few lines of sources to reflect your requirement.