Check if electron app is launched with admin privileges on windows Check if electron app is launched with admin privileges on windows windows windows

Check if electron app is launched with admin privileges on windows


I checked into how to do this from Node and found this answer: How to know if node-webkit app is running with Administrator/elevated privilege?.

I checked into the answer, downloaded node-windows and tried it. The solution, however, brought up the UAC dialog and always responded with "The user has administrative privileges".

I dug into the node-windows code that handles the isAdminUser command and found that it tried to run NET SESSION and, if does not have privilege, tries to run it elevated causing the UAC dialog.

I pulled out the part that does the elevate and ended up with this snippet:

var exec = require('child_process').exec; exec('NET SESSION', function(err,so,se) {      console.log(se.length === 0 ? "admin" : "not admin");    });

I tested this by running the application normally and with "Run as Administrator". The code above correctly displayed "not admin" when not run as administrator and "admin" when run as administrator.

This should work for the content of your .isAdminPrivilegesUsed method you referenced in the question.


You can now specify that an app should run with elevated privileges using the electron build tools:

electron-builder

Add the following to your package.json:

  "build": {    "win": {      "requestedExecutionLevel": "highestAvailable"    }  },

highestAvailable or requireAdministrator available. For full details, see: https://www.electron.build/configuration/win.html#WindowsConfiguration-requestedExecutionLevel

electron-packager

When you call electron-packager add the following command-line parameter:

--win32metadata.requested-execution-level=highestAvailable

highestAvailable or requireAdministrator available. For full details, see https://electron.github.io/electron-packager/master/interfaces/electronpackager.win32metadataoptions.html#requested_execution_level

Note

These options make the program request elevated privileges rather than check whether the program is running with administrator privileges.


If you are using electron-packager, just add --win32metadata.requested-execution-level=requireAdministrator. Eg:

electron-packager app --asar=true --platform=win32 --arch=ia32 --win32metadata.requested-execution-level=requireAdministrator --overwrite