Check to see if user did "Run as Administrator" when launching app

Started by stevengraff, February 04, 2014, 05:22:50 AM

Previous topic - Next topic

stevengraff

Can I detect, within a compiled script if, when the user ran it, if they did a right-click and chose "Run as Administrator?"

Better yet, will one of the compiler options have the effect of "forcing" this behavior?

I find that the wntSvcCreate command doesn't work otherwise.

ChuckC

As you discovered, interacting with the SCM [Service Control Manager] requires administrator level access.

The first thing that I would recommend is to compile your script to an EXE with a manifest that requires administrator level access so that it simply cannot be executed at all w/o running elevated.  That way, you don't have to make any checks in your code.  If you don't compile your script, then you have to rename it so that extension name matches a particular build of the WIL Interpreter that is manifested so that elevation is prompted for when you double-click on your script file in the Windows Explorer.  However, this method still has a problem if you are running on a version of Windows prior to Vista/Win2K8 as it would still be possible to run it from a user account with something less than administrator level access.

To make it more reliable, you can always put in a call to wntUserSidChk() in the Win32/NT Extender.  The sample code in the help topic for that function demonstrates how to determine if a particular SID is present in the user's access token.  As long as the assumption holds true that you have administrator level access when the BUILT-IN\Administrators group SID is present in your access token, then the sample code is exactly what you need.  The SID for BUILT-IN\Administrators is S-1-5-32-544, and this does not vary between domains or between member servers/workstations or even standalone servers/workstations.  If that SID is in the user's access token, then the user has administrator level access to the system; otherwise, the user is not running the script at an elevated level.