tanamonの稀に良く書く日記

KEEP CALM AND DRINK BEER

IEのプロキシ設定を書き換えるVBScript

Selenium RCでテストをしていると正常に終了されない場合にプロキシ設定が書き換わったままのことがあって面倒なのでスクリプトを書いた。JUnitとかから呼び出すとそれとなく便利。

switch_proxy.vbs

Set objShell = WScript.CreateObject("WScript.Shell")
base = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"

Rem useage
If 0 = WScript.Arguments.Count() Then
  WScript.Echo "switch_proxy.vbs on proxy.example.com:8080"
  WScript.Echo "switch_proxy.vbs off"
  WScript.Quit
End If

switch = WScript.Arguments.Item(0)

Rem proxy off
If switch = "off" Then
  WScript.Echo "switch_proxy.vbs off"
  objShell.RegWrite base + "ProxyEnable", 0, "REG_DWORD"
  WScript.Quit
End If

Rem proxy on
If switch = "on" Then
  If 2 = WScript.Arguments.Count() Then
    server = WScript.Arguments.Item(1)
    WScript.Echo "switch_proxy.vbs on " + server
    objShell.RegWrite base + "ProxyEnable", 1, "REG_DWORD"
    objShell.RegWrite base + "ProxyOverride", "<local>"
    objShell.RegWrite base + "ProxyServer", server
    WScript.Quit
  End If
End If

使い方

プロキシをオフにする

cscript switch_proxy.vbs off

プロキシを設定する

cscript switch_proxy.vbs on proxy.tanamon.jp:8080