VBS Scripting for Admin Detection

Products and tips

Moderator: Site Mods

Post Reply
Kilmatead
Platinum Member
Platinum Member
Posts: 4578
Joined: 2008 Sep 30, 06:52
Location: Dublin

VBS Scripting for Admin Detection

Post by Kilmatead »

Being a beginner at scripting, and never using UAC myself, I had always rather assumed that if you ran a script that needed Admin permissions, the wscript module would be intelligent enough to ask for it.  As it turns out, this doesn't happen - the script itself will continue to execute but those actions which require elevation will silently fail.  That's really rude - to just silently fail - not even a last gasp of air before slithering off the mortal coil.

To make matters worse, if you tell a user "Oh, be sure to run the script as Admin" they'll have to use an elevated console window, as the Windows Context Menu does not add the convenient "Run as Admin" to VBS files.

So, I went searching for a better method.  Thankfully, someone wrote the incredibly tiny CSI_IsAdmin() script which can be used as a function in VBS for all windows-based machines to check if the current user is running as Admin or not.

It simply reports True or False, it won't actually elevate the script or anything useful like that - so if you're lazy you can just check the status and have your script error-out telling the user to re-run it as admin.  This, while crude, at least prevents the script from "appearing" to run with nothing happening.

If, on the other hand, you're not lazy, you can re-spawn the same script via the ShellExecute 'runas' delimiter automatically and Windows will dutifully ask the user for UAC permission before continuing, which is the way it should be.  For completeness, you should also re-create the entire found command argument string parameters list and pass it along just in case the script has any (it would be somewhat silly not to).

Basically, just copy this (and download IsAdmin from the link above) and paste it at the top of your script (if you know it requires Admin) and it will take care of the rest.

Code: Select all

if not CSI_IsAdmin then
	Dim strCollate, param, objShell, Quote

	Quote = """"
    	strCollate = ""
	for param = 0 to Wscript.Arguments.Count - 1
		strCollate = strCollate + " " & Quote & Wscript.Arguments(param) & Quote
	next

	Set objShell = CreateObject("Shell.Application")
		objShell.ShellExecute "wscript.exe",  Quote & Wscript.ScriptFullName & Quote _
			& strCollate, "", "runas", 1

	Wscript.Quit
end if
For anyone interested, I wrote this for the x2ResUpdater utility, but if you actually look at the whole of that script this code appears at line 232 and not at the top of the script, as I just suggested.  Why?  Well, most complex scripts will do some type of error checking for their own basic requirements before actually running (is the folder passed as an argument a valid one? - is the right file-type selected? - can the user access this folder? etc).  If you put the IsAdmin() check at the top of the script, it will needlessly ask the user for elevation privileges every time it is run, just to error-out and quit immediately because the user forgot to enter his name or something stupid.  Considering that this can happen five or six times (if you have a brain-dead user) before the script actually gets to the point where it actually needs elevation, the user will get sick of UAC and just give up.  So, out of politeness, if you can write in such a way as the script can be re-run automatically after passing all error checks, it only asks permission once before doing what it wants to.  Granted, this is not always possible, but personally I'd consider it "good practice". :D

And yes Fred, while my beloved ruins are covered in ice and mist all week for the unnaturally cold temperatures we're getting, I've been really really bored, as it's surprisingly difficult to do horticultural stuff when the world is frozen around you. :wink: (Normally this time of year it's admittedly a "bit cold" but never frozen.)

Image
User avatar
fgagnon
Site Admin
Site Admin
Posts: 3737
Joined: 2003 Sep 08, 19:56
Location: Springfield

Post by fgagnon »

Very cool, Mr. Frustrated Genius  ;)
Kilmatead
Platinum Member
Platinum Member
Posts: 4578
Joined: 2008 Sep 30, 06:52
Location: Dublin

Post by Kilmatead »

fgagnon wrote:...Frustrated
Me no genius, I just like confusing Nikos with the "many faces of Janus" thing - just when he thinks I'm pigeon-holed into being an annoying guy who writes overly-long smart-arse comments, I "try" to flip the issue and do something vaguely intelligent. :D

Anyone know of any type of gloves you can type in?  My cottage is bloody freezing, the firewood's all wet, and my fingers hurt...
User avatar
nikos
Site Admin
Site Admin
Posts: 15791
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Post by nikos »

overclock the computer by a couple of notches then your oversized PC will actually have a real use!
Kilmatead
Platinum Member
Platinum Member
Posts: 4578
Joined: 2008 Sep 30, 06:52
Location: Dublin

Post by Kilmatead »

Strangely enough, I've already done that - you'd think four processors (under heavy use) each kicking out 70+ degrees Celsius would provide a nice little space-heater, wouldn't you?  A bit disappointed, was I. :sad:

That said, graphics cards under strain can produce rather prodigious quantities of thermodynamically excited air molecules.  Maybe I should just play more games and leave this programming nonsense to the professionals?

(The pipes are beginning to freeze, too.  A Christmas without water is - well - about as exciting as it sounds.)
Post Reply