I have a JScript that is designed to run on a Windows platform in a .js file (not a web browser script.) Can I detect if my script is running elevated, or with administrative privileges?
Asked
Active
Viewed 739 times
2
-
What, exactly, are you trying to check for? Which/what privilege in particular? – Jeremy J Starcher Jun 15 '13 at 04:34
-
@JeremyJStarcher: That my script is running as an administrator. – c00000fd Jun 15 '13 at 04:35
-
1Similar: [VBScript: Check if the script has administrative permissions](http://stackoverflow.com/q/1599567/113116). – Helen Jun 17 '13 at 09:59
1 Answers
0
OK. I think I got it. Thanks to this poster, this seems to work:
function isRunningElevated()
{
//Checks if this scrpt is running elevated
//RETURN:
// = 'true' if yes
var res = runCommandNoWindow("net session");
return res === 0;
}
function runCommandNoWindow(strCmd)
{
//Run command
//RETURN:
// = Integer returned by the command
var res = null;
try
{
var oShell = WScript.CreateObject("WScript.Shell");
res = oShell.Run(strCmd, 0, true); //No window, wait to finish!
}
catch(e)
{
//Failed
res = null;
}
return res;
}
-
If you search for 'net session access denied', you will see this question asked fairly frequently. If Microsoft decides to make 'net session' work without being elevated, this technique to determine elevation will no longer work. – Bill_Stewart Jun 18 '13 at 15:05
-
@Bill_Stewart: I understand that, but what is your alternative solution? I cannot get to a user's access token from JScript. On the other hand, I doubt that MS will let any user execute `net session` -- wouldn't it be a security loophole? Not that MS doesn't have any :) – c00000fd Jun 18 '13 at 16:10
-
I wrote an external executable called IsElevated32.exe that lets you determine whether the current process is running elevated. You can download it from my [web site](http://www.westmesatech.com/misctools.html). The download is "Elevation Toolkit." Unrestricted freeware. – Bill_Stewart Jun 18 '13 at 18:33
-
@Bill_Stewart: Yes, I can do it in no time with C++ or C#. The question here is how to do it in a Windows Host scripting language that doesn't have access to your executable file. If I had, I'd do all the work there. – c00000fd Jun 18 '13 at 20:07
-
There is not a "native" way to detect elevation in a WSH script without using a side-effect such as the "access denied" result from running **net session**. – Bill_Stewart Jun 18 '13 at 20:26