Automatically Check the Proxy Switch in IE





3.00/5 (2 votes)
A small VBScript which is used to automatically set the proxy switch, for IE, in the Registry.
Introduction
This is my first submission, so I thought it had better be something that I have found useful.
Here is a a small bit of VBScript which is used to automatically set the proxy switch, for IE, in the Registry.
The script checks for a connection to a specific proxy server "SERVERNAME". If the server is found, it will set the switch in the Registry. If the server isn't found, it will disable the switch. The script then waits 60 seconds and then calls itself.
Background
Where I work, we have just done a laptop roll-out for all of our sales staff, which included setting them up with a VPN connection to our corporate network. We use a proxy server within the network, and this causes a few issues for some users when they disconnect from the VPN but still want to use IE to browse the web.
I have been asked many times why the users have been experiencing problems connecting to the internet when they are not connected to the VPN. I therefore decided to look into a way of automatically updating the proxy switch in Internet Explorer so any changes in the connection to the VPN would be almost invisible to the user.
I spent quite a while finding bits of code on the Scripting Guy website that would be useful, but didn't find an article with everything I needed, so I wrote this script.
Using the Code
Simply copy this code into Notepad, change "SERVERNAME" to the name of your proxy server, and then save it in the Startup folder in your Start menu, with a '.vbs' extension.
All of the comments in the script should give you an idea of what's going on.
Const HKEY_CURRENT_USER = &H80000001
Const OK_BUTTON = 0
Const AUTO_DISMISS = 0 'On Error Resume Next
'Used for local computer name value can be changed for remote changes
strComputer = "."
strServer = "SERVERNAME" 'change this to your Proxy server name
Set objShell = CreateObject("Wscript.shell")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
'Registry key path
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"
strValueName = "ProxyEnable" ' Key name
' Gets the registry value
objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
' I can't even remember where the following lines came from.
' Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' Set colItems = objWMIService.ExecQuery _
' ("Select * from Win32_PingStatus " & _
' "Where Address = '" & strServer & "'")
blnReply = reachable(strServer) 'calls the reachable function
If blnReply = true And dwValue = 0 Then 'if the values do not match change them
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
'objShell.Popup "Proxy server found." & vbCrLf & _
' "Settings have been updated automatically." , _
' AUTO_DISMISS, "Internet Proxy Manager", OK_BUTTON
ElseIf blnReply = false And dwValue = 1 Then 'other way round check
dwValue = 0
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
'objShell.Popup "Proxy server not found." & vbCrLf & _
' "Settings have been updated automatically." , _
' AUTO_DISMISS, "Internet Proxy Manager", OK_BUTTON
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Replace(Wscript.ScriptFullName," ","%20"))
Do Until objFSO.FileExists(Wscript.ScriptFullName)
WScript.Echo("""" & Wscript.ScriptFullName & """ Does Not Exist!")
Wscript.Sleep 10000
Loop
Wscript.Sleep 60000' Sleep for 60 seconds
'"setProxy.vbs"
objShell.Run """" & Wscript.ScriptFullName & """" 'Wscript.ScriptFullName
function reachable(HostName) ' Checks for a connection to the proxy server
'This function pings the server and then reads
'the results to see if anything was returned
dim wshShell, fso, tfolder, tname, TempFile, results, retString, ts
Const ForReading = 1, TemporaryFolder = 2
reachable = false
set wshShell=wscript.createobject("wscript.shell")
set fso = CreateObject("Scripting.FileSystemObject")
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
tname = fso.GetTempName
TempFile = tfolder & tname
wshShell.run "cmd /c ping -n 3 -w 1000 " & HostName & ">" & TempFile,0,true
set results = fso.GetFile(TempFile)
set ts = results.OpenAsTextStream(ForReading)
do while ts.AtEndOfStream <> True
retString = ts.ReadLine
if instr(retString, "Reply")>0 then
reachable = true
exit do
end if
loop
ts.Close
results.delete
end function
Points of Interest
There is probably an easier way of doing the above, but I didn't find it. Let me know if you have any better methods. There are so many things that can be done with a bit of VBScript knowledge. The Microsoft Scripting Guy is an extremely valuable resource when trying to do stuff like this.
History
This is the first version. I may add extra functionality to update the Registry with the correct proxy settings if they have been altered.