Does ServiceController.start () and ServiceController.stop () throw exceptions?

The following code throws an exception. I do not understand what error I am making in the code. Can someone help me sort it out, please. I think this is a security issue. If so, how can I grant security rights to any user or application to programmatically access this Windows service?

Dim sc As New ServiceController sc.ServiceName = "DataLoad" If sc.Status = ServiceControllerStatus.Stopped Then sc.Start() Else sc.Stop() End If 

Exception

 System.InvalidOperationException: Cannot open DataLoad service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied --- End of inner exception stack trace --- at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at WEBSITE.DataLoad.Submit1_ServerClick(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WEBSITE\a\DataLoad.aspx.vb:line 46 

Thanks!

+2
source share
5 answers

You can use subinacl tool for this

 SUBINACL /SERVICE \\MachineName\ServiceName /GRANT=[DomainName\]UserName[=Access] 

To be precise for your case:

 subinacl /service DataLoad /GRANT=YOURDOMAIN\[User in appdomain for WEBSITE]=TO 

Where TO stands for T: start service
O: Service stop

All options for [Access]:

F: full control
R: General reading
W: Generic Write
X: generic eXecute
L: Read controL
Q: Query service configuration
S: Query Service Status
E: List dependent services
C: Change service settings
T: start service
O: Stop the service
P: Pause / Continue Service
I: Survey Service
U: Utility User Management Commands

See Method 3 in this kb article

+2
source

I found a solution to this problem by providing the machine name of the machine that currently runs the service in the overloaded ServiceController constructor, which takes 2 (two) arguments, i.e. public ServiceController (/ my service name string /, System.Environment.MachineName / this machine executing the service /)

The .Net version of this solution was tested on 4.5, I hope this helps anyone who is still looking for a solution.

Here is what you need to do in the code:

 ServiceController serviceController = new ServiceController("myServiceName", System.Environment.MachineName); 
+1
source

GetServiceHandle requires some permissions. If it works when you run it as an Admin user, but not as a regular user, then perhaps this article can help.

It clearly shows how to manually grant Windows user rights to start and stop services (or set other permissions):

http://thommck.wordpress.com/2011/12/02/how-to-allow-non-admins-to-start-and-stop-system-services/

+1
source

In my case, I decided that I needed to configure security on my service so that it could be restarted by a separate watchdog service if my service failed.

First, open the mmc.exe file, then add the Security Configuration and Analysis snap-in and Security Templates.

enter image description here

Then create a new empty security template from the "Security Templates" element, give it a name and click "OK" to save it to your local disk somewhere conveniently.

Then open "Configuration and Security Analysis" and select "Open Database ...", give it a name and save it in the same directory as the previous step. When the "Import Template" window appears, open the * .inf file in the same directory.

Then right-click "Configuration and Security Analysis" and select "Computer Analysis ...". The following message will appear:

enter image description here

Double-click "System Services", locate and double-click your service, then select the "Define this policy in the database" check box and click the "Change Security" button.

Here it becomes different than what is described in the @JOG link published since I use Windows 8.1. I have included "start, stop and pause" for "INTERACTIVE" and "SERVICE"

enter image description here

FYI, I did this following this guide as @JOG suggested: https://thommck.wordpress.com/2011/12/02/how-to-allow-non-admins-to-start-and-stop-system -services /

0
source

If you already have a service user like LocalSystem (high-privilege user), the problem is not security. Also, I had this problem before and its status vrs starts it again or stops it when commande already stops ().

You see that the status of the service does not change on demand, so even if you have encoded

 //this will start the process but the //sc status will take some time to change //when that happens and you try to start //the already started service it will give you //your error servicec.start(); 

Su, you need to do this: msdn ServiceController.waitforstatus

  Dim sc As New ServiceController sc.ServiceName = "DataLoad" If sc.Status = ServiceControllerStatus.Stopped Then sc.Start() // this makes it wait for the status to change // and no it wont slow down anything at all. sc.waitforstatus(ServiceControllerStatus.started) Else sc.Stop() sc.waitforstatus(ServiceControllerStatus.stopped) End If 

This will solve your problem, as well as mine.

-1
source

All Articles