Archive

Archive for December, 2010

Windows proxy control script

December 2nd, 2010 No comments

Here’s a simple script that lets you toggle on or off your internet proxy in Windows. Save it to a file called Proxy.vbs on your desktop and double-click the file to execute the script.

As shown below, the script will not change the proxy server address, but will simply turn it ON or OFF. If you want to also modify the proxy server and exception list every time you turn it on, update lines 37 and 38, and uncomment lines 40-41.

Parts of this script come from a comment someone posted on a web site, but I no longer have the link to provide proper attribution.

Const HKCU=&H80000001 'HKEY_CURRENT_USER
Const HKLM=&H80000002 'HKEY_LOCAL_MACHINE

Const REG_SZ		= 1
Const REG_EXPAND_SZ	= 2
Const REG_BINARY	= 3
Const REG_DWORD		= 4
Const REG_MULTI_SZ	= 7
Const HKCU_IE_PROXY	= "Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Set oReg=GetObject("winmgmts:!root/default:StdRegProv")

Main

Sub Main()
	buttons = vbQuestion + vbYesNoCancel + vbDefaultButton1

	If GetValue(HKCU,HKCU_IE_PROXY,"ProxyEnable",REG_DWORD) = 1 AND _
	Len(GetValue(HKCU,HKCU_IE_PROXY,"ProxyServer",REG_SZ)) > 0 Then
		' If Proxy is set then default to turning it off
		buttons = buttons + vbDefaultButton2
	End If

	choice = MsgBox("Enable the internet proxy?", buttons, "Proxy setting")
	If choice = vbYes Then
		SetProxy True
	ElseIf choice = vbNo Then
		SetProxy False
	End If
End Sub

Sub SetProxy(enabled)
	If enabled = False Then
		CreateValue HKCU,HKCU_IE_PROXY,"ProxyEnable",0,REG_DWORD
		MsgBox "Proxy Disabled", vbInformation, "Proxy OFF"
	Else
		strProxyServer = "MyProxySvr:80"
		strProxyOveride = "*.domain.com;*.domain2.com;*domain3.com"
	
		'CreateValue HKCU,HKCU_IE_PROXY,"ProxyServer",strProxyServer,REG_SZ
		'CreateValue HKCU,HKCU_IE_PROXY,"ProxyOverride",strProxyOveride,REG_SZ
		CreateValue HKCU,HKCU_IE_PROXY,"ProxyEnable",1,REG_DWORD
		MsgBox "Proxy Enabled and set to:" & vbcrlf & "(" & strProxyServer & ")", vbInformation, "Proxy ON"
	End If
End Sub

Function CreateValue(Key,SubKey,ValueName,Value,KeyType)
	Select Case KeyType
	Case REG_SZ
		CreateValue = oReg.SetStringValue(Key,SubKey,ValueName,Value)
	Case REG_EXPAND_SZ
		CreateValue = oReg.SetExpandedStringValue(Key,SubKey,ValueName,Value)
	Case REG_BINARY
		CreateValue = oReg.SetBinaryValue(Key,SubKey,ValueName,Value)
	Case REG_DWORD
		CreateValue = oReg.SetDWORDValue(Key,SubKey,ValueName,Value)
	Case REG_MULTI_SZ
		CreateValue = oReg.SetMultiStringValue(Key,SubKey,ValueName,Value)
	End Select
End Function

Function DeleteValue(Key, SubKey, ValueName)
	DeleteValue = oReg.DeleteValue(Key,SubKey,ValueName)
End Function

Function GetValue(Key, SubKey, ValueName, KeyType)
	Dim Ret

	Select Case KeyType
	Case REG_SZ
		oReg.GetStringValue Key, SubKey, ValueName, Value
		Ret = Value
	Case REG_EXPAND_SZ
		oReg.GetExpandedStringValue Key, SubKey, ValueName, Value
		Ret = Value
	Case REG_BINARY
		oReg.GetBinaryValue Key, SubKey, ValueName, Value
		Ret = Value
	Case REG_DWORD
		oReg.GetDWORDValue Key, SubKey, ValueName, Value
		Ret = Value
	Case REG_MULTI_SZ
		oReg.GetMultiStringValue Key, SubKey, ValueName, Value
		Ret = Value
	End Select

	GetValue = Ret
End Function
Categories: Uncategorized Tags: ,