Search code examples
windowsbatch-filecmdadmin

Start a service from a webapp with administrator privileges passing by a batch file


I'm working on a monitoring app that is running on a vm and I want my colleagues to be able to use it without connecting on the vm. It works fine but I have one problem.

One of my goals is to start/stop services using a batch file or a cmd file (using Class Runtime in my back). The problem is that I need the administrator privileges to do that. All the methods I found (as Runas or some batch files) only works if the user is on the vm and enter the password or click on the "do you want to run this application as administrator" pop up. I would like to not use the task scheduler to always start cmd as an admin. I already know you can use softwares as "Sanur" but I prefer not to.

The only way to make it works is to start the app as an admin and it works fine (I'm using the "net start/stop" command). But I would like to let everyone start this app (in case I'm not here). It's not a problem to write the login/password in the file.

Is there a way to do so?

The vm runs on Windows 7.

Thank you in advance.


Solution

  • You don't need to be Administrator to start and stop a service, you just need the privilege. With Windows 7 Professional you can do this through the built-in group policy editor.

    With Windows 7 Home, there is not policy editor.

    I use a tool called SetACL to do this, e.g:

    setacl -on "service-name" -ot srv -actn ace -ace "n:betrand;p:start_stop"
    

    Where "service-name" is the name of the service, and betrand is the user which you want to allow to start and stop the service.

    A complete list of options is available on the website:

    Another option is the SubInACL tool directly from Microsoft.