Script Sharp 1 – Scripting Internet Explorer proxy configuration

This is for those who continuously have to configure Internet Explorer proxy settings to match different network locations.

I have two different configurations I’m switching at least twice a day: one for when I’m in the office and another for my home WI-FI network. Not to talk about the number of configurations I have for the customers I sometimes have to visit.

Script must be included in a .vbs file that you can call directly from the shell or with a shortcut.

Here the code you can run to enable/disable proxy:

On Error Resume Next

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
strKeyPath = "SoftwareMicrosoftWindowsCurrentVersionInternet Settings\"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}" & strComputer & "rootdefault:StdRegProv")
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, "ProxyEnable", 1 'Use 0 to disable proxy
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, "ProxyServer", "proxyName:8080"

If err.Number <> 0 Then
    MsgBox err.Description
End If

MsgBox "Done!"

Note that you can work on a different machines (strComputer) and impersonate a desired user (the WMI string passed to GetObejct() function), but I haven’t investigated this yet.

Advertisements

~ by Matteo on March 12, 2007.

4 Responses to “Script Sharp 1 – Scripting Internet Explorer proxy configuration”

  1. But why don’t you use a proxy.pac?

    I have one on my pc, that automatically switches proxy depending on the IP of my primary network adapter. Thus, the choice of the correct proxy is somewhat automatic. The file I use is similar to this one:

    function FindProxyForURL(url,host) {
    if(isPlainHostName(host))
    return "DIRECT";
    else if(isInNet(host,"127.0.0.0","255.0.0.0"))
    return "DIRECT";
    else if(isInNet(host,"192.168.0.0","255.255.0.0")) // VMWare & Wi-Fi
    return "DIRECT";
    else if(isInNet(myIpAddress(),"172.16.0.0","255.255.0.0")) // BF
    return "PROXY 172.16.0.170:8080;DIRECT";
    else
    return "DIRECT";
    }

    I even found that you do not need an http service (i.e. a local IIS or Apache) to give back the .pac file: you can simply use a URL syntax with the “file:” prefix, like:
    file:///drive/path/proxy.pac

    This is the configuration for Firefox, I think that with IE you must use only two slashes after the “file:”. I think that if you have any http service on your pc, it is better to specify an “http://localhost/…” path to reach your proxy.pac file, because some other tools that use the IE proxy can get confused by the “file:” syntax…

  2. Wonderful! the final solution.

  3. I get “Object Required” when using

    On Error Resume Next

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = “.”
    strKeyPath = “SoftwareMicrosoftWindowsCurrentVersionInternet Settings\”
    Set objReg = GetObject(“winmgmts:{impersonationLevel=impersonate}” & strComputer & “rootdefault:StdRegProv”)
    objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, “ProxyEnable”, 1 ‘Use 0 to disable proxy
    objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, “ProxyServer”, “192.168.100.254:8080”

    If err.Number 0 Then
    MsgBox err.Description
    End If

    MsgBox “Done!”

  4. Andre, probably what’s failing on your machine is the call to GetObject(). But it’s hard to say why, may be a permission or something related to UAC (in case you are on Vista/Windows7)?

    I don’t know, but in any case I recommend you following the advise by Pietro: use the proxy.pac method. It’s simply much better.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: