Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remotely control a Windows Service with ServiceController?

Tags:

I'm trying to control Windows Services that are installed in a remote computer. I'm using the ServiceController class.

I have this:

ServiceController svc =  new ServiceController("MyWindowsService", "COMPUTER_NAME");

With this, I can get the status of the Windows Service like this:

string status = svc.Status.ToString();

But I can't control the Windows Service (by doing svc.Start(); or svc.Stop();). I get the following exception:

Cannot open Servicexxx service on computer 'COMPUTER_NAME'

That's normal, I suppose there is something to do with access permissions. But how? I've looked into Google but didn't find what I was looking for. However I often read something related to impersonation, but I don't know what that means.

NB: The local and remote computers are both running Win XP Pro.

like image 344
Amokrane Chentir Avatar asked Jun 08 '10 09:06

Amokrane Chentir


People also ask

How do I remotely connect to a Windows service?

To connect to a remote services MMC, click the Services name in the left pane, go to Action, then Connect to another computer… Once connected, you can operate the services just like you do on the local system.


2 Answers

Problem solved.

Impersonation consists in running a piece of code using a certain logon/password. I found this very useful project: http://www.codeproject.com/KB/cs/svcmgr.aspx?display=Print that helped me a lot!

like image 60
Amokrane Chentir Avatar answered Sep 26 '22 04:09

Amokrane Chentir


Starting and stopping services is a highly privileged operation, normally available only to administrators. Ensure that the user account you use has sufficient privileges on the target machine. Ask more questions about it at serverfault.com

like image 8
Hans Passant Avatar answered Sep 26 '22 04:09

Hans Passant