VBScript - IE Proxy Setting

Environment
  • Microsoft Windows XP
Version
2009-NOV-02
  • Proxy service via Auto Configuration Script

Codes

'Default value
Const DEFAULT_TRUE = "TRUE"
Const DEFAULT_FALSE = "FALSE"

'Automatic configuration script
Const DEFAULT_AUTOCONFIG_URL = "http://192.168.0.253/proxy.pac"

'Proxy server setting
Const DEFAULT_PROXY_SERVER = "192.168.0.254:8080"
'<local> is for bypass proxy server for local addresses
Const DEFAULT_PROXY_OVERRIDE = "192.168.0.*;<local>"

'Parameter
'0: show options
'1: automatic configuration script
'2: bypass proxy server
'3: proxy server
'4: not use proxy service
DEFAULT_PARA = Array("?", "a", "b", "p", "n")

'Regedit
Const DEFAULT_REG = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"
DEFAULT_AUTOCONFIG_REG = DEFAULT_REG & "AutoConfigURL"
DEFAULT_PROXYSERVER_REG = DEFAULT_REG & "ProxyServer"
DEFAULT_PROXYOVERRIDE_REG = DEFAULT_REG & "ProxyOverride"
DEFAULT_PROXYENABLE_REG = DEFAULT_REG & "ProxyEnable"

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set args = WScript.Arguments.Named

'Check if it has a auto config proxy parameter
'If not, return false
'Return String AutoConfigProxy
Function autoConfigProxy()
    autoConfigProxy = getParameter(DEFAULT_PARA_AUTOCONFIG)
End Function

'Disable proxy of IE
Sub disableProxyService()
    On Error Resume Next
    'Delete this for not using automatic configuration script
    WSHShell.RegDelete DEFAULT_AUTOCONFIG_REG
  
    WSHShell.RegWrite DEFAULT_PROXYENABLE_REG, 0, "REG_DWORD"
End Sub


'Show help options
Sub getOptions()
    WScript.Echo "Edit Proxy setting of Internet Explorer"
    WScript.Echo ""
    WScript.Echo "/" & DEFAULT_PARA(0) & "    Display help message"
    WScript.Echo ""
    WScript.Echo "/" & DEFAULT_PARA(1) & "    Select to use which automatic configuration script"
    WScript.Echo "    If use this, not allow to select which proxy server"
    WScript.Echo "    e.g. http://127.0.0.1/proxy.pac"
    WScript.Echo ""
    WScript.Echo "/" & DEFAULT_PARA(2) & "    Which sites or ips are no needed to visit via proxy server,"
    WScript.Echo "    e.g. ""192.168.0.*;<local>"""
    WScript.Echo "    <local> is for bypass proxy server for local addresses"
    WScript.Echo ""
    WScript.Echo "/" & DEFAULT_PARA(3) & "    Select which proxy server, e.g. localhost:8080"
    WScript.Echo "    The port also should be provided"
    WScript.Echo "    If use this, not allow to select which automatic configuration script"
    WScript.Echo ""
    WScript.Echo "/" & DEFAULT_PARA(4) & "    Not use proxy service"
    WScript.Echo ""
    WScript.Echo "Example:"
    WScript.Echo "    Use auto configuration script:"
    WScript.Echo "    cscript ProxyConf.vbs /a:http://127.0.0.1/proxy.pac"
    WScript.Echo ""
    WScript.Echo "    Use proxy server:"
    WScript.Echo "    cscript ProxyConf.vbs /p:w.x.y.z:8080 /b:""10.*;"""
    WScript.Echo ""
    WScript.Echo "    Not use proxy server:"
    WScript.Echo "    cscript ProxyConf.vbs /n"
End Sub

'Get the parameter value
'If no parameter value, return parameter
'If not here, return false
Function getParameter(par)
    If args.Exists(par) Then
        If IsEmpty(args.Item(par)) Then
            'Return the parameter as showing ?
            getParameter = par
        Else
            getParameter = args.Item(par)
        End If
    Else
        getParameter = DEFAULT_FALSE
    End If
End Function

'Check if it has a server parameter
'If not, return false
'Return String Proxy Server
Function proxyServer()
    proxyServer = getParameter(DEFAULT_PARA_PROXYSERVER)
End Function

'Check if it has a override parameter
'If not, return false
'Return String Proxy Override
Function proxyOverride()
    proxyOverride = getParameter(DEFAULT_PARA_BYPASS)
End Function

'Set which automatic configuration script
Sub setAutoConfig(autoConfig)
    WSHShell.RegWrite DEFAULT_AUTOCONFIG_REG, autoConfig
End Sub

'Set Which Proxy Server and which one is needed to override
Sub setProxyServer(proxyServer, proxyOverride)
    WSHShell.RegWrite DEFAULT_PROXYSERVER_REG, proxyServer
  
    If proxyOverride <> DEFAULT_FALSE Then
        'Localhost and Intranet, no need via Proxy Server
        WSHShell.RegWrite DEFAULT_PROXYOVERRIDE_REG, proxyOverride
    End If
  
    'Enable to use it
    WSHShell.RegWrite DEFAULT_PROXYENABLE_REG, 1, "REG_DWORD"
End Sub

'Check if it has a enable proxy parameter
'If not, return false
'Return int
Function useProxy()
    useProxy = getParameter(DEFAULT_PARA_USEPROXY)
End Function

'Check if wanna to show help
'Return boolean Show
Function showOptions()
    If args.Exists("?") Then
        showOptions = DEFAULT_TRUE
    Else
        showOptions = DEFAULT_FALSE
    End If
End Function

'Main process
Function main()
    options = getParameter(DEFAULT_PARA(0))
    autoConf = getParameter(DEFAULT_PARA(1))
    server = getParameter(DEFAULT_PARA(3))
    override = getParameter(DEFAULT_PARA(2))
    notUse = getParameter(DEFAULT_PARA(4))
  
    If options = DEFAULT_FALSE Then
        If notUse = DEFAULT_FALSE Then
            If autoConf = DEFAULT_FALSE Or _
                autoConf = DEFAULT_PARA(1) Then
                    If server = DEFAULT_FALSE Or _
                        server = DEFAULT_PARA(3) Then
                            If notUse = DEFAULT_PARA(2) Then
                                disableProxyService()
                            Else
                                getOptions()
                            End If
                    Else
                        Call setProxyServer(server, override)
                    End If
            Else
                Call setAutoConfig(autoConf)
            End If
        Else
            disableProxyService()
        End If
    Else
        getOptions()
    End If
End Function

main()