vbscript - Prompt user before toggling proxy -
i need little vbscript using disable , enable proxy settings. happen script tells user current setting proxy either off or on if want can click change yes/no. or proxy off or proxy on.
i know how make message box dont know should put code.
this text box code:
result = msgbox("proxy set off", vbokonly+vbinformation, "")
and proxy changing code:
option explicit dim wshshell, strsetting set wshshell = wscript.createobject("wscript.shell") 'determine current proxy setting , toggle oppisite setting strsetting = wshshell.regread("hkcu\software\microsoft\windows\currentversion\internet settings\proxyenable") if strsetting = 1 noproxy else end if 'subroutine toggle proxy setting on sub proxy wshshell.regwrite "hkcu\software\microsoft\windows\currentversion\internet settings\proxyenable", 1, "reg_dword" end sub 'subroutine toggle proxy setting off sub noproxy wshshell.regwrite "hkcu\software\microsoft\windows\currentversion\internet settings\proxyenable", 0, "reg_dword" end sub
i've reorganised code bit should allow choose if wish toggle proxy on or off.
some of changes include;
- moving reused strings not change
constant
. - giving script single point of execution wrapping main logic in sub procedure called
main()
. make sure call procedure code run in global scope exceptwscript.shell
declaration. - converting return value of
currentproxy()
boolean makes toggling off , on behaviour easier. - using
array
store variations of words used inmsgbox()
avoid duplication of code.
option explicit dim wshshell, strsetting set wshshell = wscript.createobject("wscript.shell") 'store strings not change , reused in constants const appname = "proxy setting" const proxy_setting = "hkcu\software\microsoft\windows\currentversion\internet settings\proxyenable" 'have main procedure single point of script execution. call main() sub main() 'convert return value boolean make easy toggle. dim setting: setting = cbool(currentproxy()) dim state 'use array store wordy bits used message box. if setting state = array("enabled", "off") else state = array("disabled", "on") end if if msgbox("proxy " & state(0) & vbcrlf & "do wish switch " & state(1), vbyesno + vbquestion, appname) = vbyes 'toggle opposite of current state use not. call toggleproxy(not setting) call msgbox("proxy has switched " & state(1), vbinformation, appname) end if end sub 'determine current proxy setting , toggle oppisite setting function currentproxy() dim strsetting strsetting = wshshell.regread(proxy_setting) currentproxy = strsetting end function 'joined proxy , noproxy 1 procedure call , pass in 'proxyenable setting argument. 'subroutine toggle proxy setting on sub toggleproxy(setting) 'abs() function makes sure pass 1 or 0 boolean true in 'vbscript -1. wshshell.regwrite proxy_setting, abs(setting), "reg_dword" end sub
Comments
Post a Comment