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
strComputer = "."
strServer = "SERVERNAME"
Set objShell = CreateObject("Wscript.shell")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"
strValueName = "ProxyEnable"
objRegistry.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
blnReply = reachable(strServer)
If blnReply = true And dwValue = 0 Then
dwValue = 1
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
ElseIf blnReply = false And dwValue = 1 Then
dwValue = 0
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Do Until objFSO.FileExists(Wscript.ScriptFullName)
WScript.Echo("""" & Wscript.ScriptFullName & """ Does Not Exist!")
Wscript.Sleep 10000
Loop
Wscript.Sleep 60000
objShell.Run """" & Wscript.ScriptFullName & """"
function reachable(HostName)
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.