15
$\begingroup$

To avoid execution problems in a program I need to know if Mathematica has been "Run as administrator". What would be the cleanest way to programmatically getting this information?

$\endgroup$
  • 1
    $\begingroup$ Which OS? Windows from the wording but can you confirm? $\endgroup$ – Gordon Coale Jun 1 '15 at 16:37
  • $\begingroup$ Yes, I work on Windows. $\endgroup$ – Ariel Sepulveda Jun 1 '15 at 16:43
  • 1
    $\begingroup$ See this if you want a command line method: (4051883) $\endgroup$ – Mr.Wizard Jun 1 '15 at 17:33
  • $\begingroup$ @Mr.Wizard: Your response is clearly an answer to the question. I use Mathematica as much as possible, so I would rather not have to go somewhere else for a solution. In other words, maybe your code should be packed in a function (RunningAsAdministratorQ) and included as part of the Wolfram Language. $\endgroup$ – Ariel Sepulveda Jun 1 '15 at 17:38
  • 1
    $\begingroup$ I don't think that Mathematica has this built in (one never knows as there is a lot of stuff which is not documented or hard to find). So I think you can either a) use a indirect method as Mr.Wizard has suggested (which might of course give a wrong answer, e.g. when someone has played with permissions of $InstallationDirectory), you can b) fire a command line tool (also suggested already) or use NETLink to access the corresponding Windows API call (which I don't know but certainly exists...) $\endgroup$ – Albert Retey Jun 3 '15 at 20:49
14
$\begingroup$

The only method I am aware of is to attempt an operation that requires administrator-level permissions and look for a General::privv message:

Check[
  (CreateDirectory @ #; DeleteDirectory @ #) & @ 
   FileNameJoin[{$InstallationDirectory, "test"}]; True,
  False,
  General::privv
] // Quiet

This should return True if it is run with administrator control and False if not.

| improve this answer | |
$\endgroup$
  • $\begingroup$ This is a clean workaround. I was hoping for a function call though. $\endgroup$ – Ariel Sepulveda Jun 1 '15 at 16:51
  • $\begingroup$ @Ariel Quite likely there is a better way but at least this works in the mean time. $\endgroup$ – Mr.Wizard Jun 1 '15 at 17:32
  • 3
    $\begingroup$ +1 This is a nice direct method. Funny story: I once ran into a case where a very locked-down corporate virus scanner unceremoniously terminated my program when it tried to use a similar technique. $\endgroup$ – WReach Jun 6 '15 at 23:46
6
$\begingroup$

It may suit your purpose to use the Windows API function IsUserAnAdmin. It can be accessed through NETLink like this:

Needs["NETLink`"]
isUserAnAdmin = DefineDLLFunction["IsUserAnAdmin", "shell32.dll", "bool", {}];

isUserAnAdmin[]
(* True if Mathematica was launched elevated, False otherwise *)

It is also possible to to use the shell command whoami to determine the Mandatory Label which, in turn, can tell us if the current session is running elevated:

isElevated[] :=
  !StringFreeQ[
    Import["!whoami /groups | find \"Mandatory Label\"", "Text"]
  , "S-1-16-12288"|"S-1-16-16384"
  ]

isElevated[]
(* True if Mathematica was launched elevated, False otherwise *)

The magic strings in isElevated are the security identifiers (SIDs) denoting the elevated integrity levels. This text-based solution has the advantage that it does not require NETLink to be loaded. On the other hand, it is more brittle should the output format of whoami ever be changed.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.