Downloads
42105.zip

I spend most of my time working from a remote office or on the road. I use a VPN to access the intranet on my company's corporate network. To access external resources while I'm connected through the VPN, the proxy settings in Microsoft Internet Explorer (IE) must be enabled. When I'm not using the VPN, the proxy settings must be disabled. Because my company doesn't use proxy autodetection, I have to manually enable and disable the proxy settings. How can I script this task?

You can use a script to enable or disable the proxy settings by programmatically sending keystrokes to IE or by modifying the registry entry that controls IE's proxy settings. To programmatically send keystrokes, you need to use the Windows Script Host (WSH) SendKeys method (which you access through the WshShell object). However, using SendKeys is almost always an undesirable approach. SendKeys is fragile because it accesses the Windows GUI interface, which is characteristically dynamic. For example, certain windows might or might not be opened or the task bar might be repositioned. Scripts attempting to access this dynamic environment often fail if they don't include a lot of cumbersome error-handling code. Because accessing a specific registry entry is significantly more predictable, it's the better approach.

IE stores its configuration settings in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings registry subkey. In this subkey, the ProxyEnable entry controls enabling and disabling IE's proxy settings. Setting ProxyEnable to 0 disables them; setting ProxyEnable to 1 enables them.

You can modify ProxyEnable by using the WSH RegWrite method (which you access through the WshShell object) or the Windows Management Instrumentation (WMI) SetDWORDValue method (which you access through the StdRegProv registry provider, which is in WMI's root\DEFAULT namespace). SetDWORDValue is more complex to call than RegWrite, but SetDWORDValue, through StdRegProv, provides full write access to the registry. RegWrite's write access is somewhat limited because it can't write REG_MULTI_SZ values.

Listing 3 shows how to use RegWrite to enable IE's proxy settings. The script instantiates the WshShell object (i.e., WScript.Shell), then calls the object's RegWrite method. As the code at callout A in Listing 3 shows, RegWrite takes as many as three parameters. The two required parameters are the registry subkey and entry you want to configure (in this case, HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable) and the value you want to assign to that entry (1). The optional third parameter specifies the value's data type (REG_DWORD). To disable the proxy settings, you simply need to change the second parameter's value from 1 to 0.

Listing 4 shows how to use the SetDWORDValue method to enable IE's proxy settings. You use SetDWORDValue because the registry stores ProxyEnable as a DWORD data type. (Each registry data type has its own WMI method.) In the code at callout A in Listing 4, the script sets an object reference for StdRegProv to the objRegistry variable. The next few lines supply several parameter values for SetDWORDValue. As the code at callout B shows, SetDWORDValue requires four parameters that specify the registry subkey, entry, and value you want to configure. The parameters are

  • the subtree (in this case, HKEY_CURRENT_USER)
  • the subkey minus the subtree (Software\Microsoft\Windows\CurrentVersion\Internet Settings)
  • the entry's name (ProxyEnable)
  • the entry's value (1)

To disable the proxy settings, you just need to change the last parameter's value from 1 to 0.