windows vista - Vbscript detect whether UAC-elevated -
how can vbscript detect whether or not running in uac elevated context?
i have no problem detecting user, , seeing if user within administrators group. still doesn't answer question of whether process has elevated privs or not, when running under vista or windows 2008. please note, need detect status; not attempt elevate or (err ..) de-elevate.
the method settled on depends on fact vista , windows 2008 have whoami.exe utility, , detects integrity level of user owns process. couple of screenshots here:
you can see when cmd running elevated, whoami /groups reports "high" mandatory integrity level , different sid when running non-elevated. in pic, top session normal, 1 underneath running elevated after uac prompt.
knowing that, here code used. checks os version, , if vista or server 2008, calls checkforelevation runs whoami.exe /groups, , looks string s-1-16-12288 in output. in example echo status; in real script branch different actions based on result.
sub getosversion dim strcomputer, owmiservice, colosinfo, oosproperty, strcaption, strosfamily strcomputer = "." set owmiservice = getobject("winmgmts:\\" & strcomputer & "\root\cimv2") set colosinfo = owmiservice.execquery("select * win32_operatingsystem") 'i hate looping through 1 property. dunno way! each oosproperty in colosinfo strcaption = oosproperty.caption next if instr(1,strcaption, "vista", vbtextcompare) strosfamily = "vista" if instr(1,strcaption, "2008", vbtextcompare) strosfamily = "2008" if instr(1,strcaption, "xp", vbtextcompare) strosfamily = "xp" if instr(1,strcaption, "2003", vbtextcompare) strosfamily = "2003" if instr(1,strcaption, "2000", vbtextcompare) strosfamily = "2000" if strosfamily = "" wscript.echo "no known os found. (script can detect windows 2000, 2003, xp, vista, 2008.)" else wscript.echo "os family = " & strosfamily end if select case strosfamily 'if vista/2008 call checkforelevation case "vista" checkforelevation case "2008" checkforelevation case else exit sub end select end sub sub checkforelevation 'test whether user has elevated token dim oshell, oexecwhoami, owhoamioutput, strwhoamioutput, boolhaselevatedtoken set oshell = createobject("wscript.shell") set oexecwhoami = oshell.exec("whoami /groups") set owhoamioutput = oexecwhoami.stdout strwhoamioutput = owhoamioutput.readall if instr(1, strwhoamioutput, "s-1-16-12288", vbtextcompare) boolhaselevatedtoken = true if boolhaselevatedtoken wscript.echo "current script running elevated privs." else wscript.echo "current script not running elevated privs." end if end sub
Comments
Post a Comment