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
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. (Normally this time of year it's admittedly a "bit cold" but never frozen.)