0

I'm running a windows service in a server and I now I want it to restart it from a local machine by a software tool I developed by C#.

I tried using "sc" but it says "The service is not installed" and I think it is because it is actually not a windows default service but a custom one.

Could anyone help me out with how to do it?

P.S. sc also said I don't have enough privilege because the service is running under administrative privilege in the server.

Thanks in advance!

Tarek Saazeb
  • 35
  • 1
  • 11
  • se.exe will work for any correctly configured service. You can use `ServiceController()` to accomplish the same thing. – Alex K. Oct 17 '16 at 13:59
  • I edited the question. I tried doing so, it didn't work because the service is running under another user account. @AlexK. – Tarek Saazeb Oct 17 '16 at 14:02

1 Answers1

0

You can use something like the following to start and stop a service in a remote location

ServiceController insServiceController = new ServiceController("Your Service Name", "IP address of the remote server");

insServiceController.Start();
insServiceController.Stop();
Ozan Gunceler
  • 1,067
  • 11
  • 20
  • I edited the question. The service is running under another user account with admin privilege. This solution won't work. @Ozan – Tarek Saazeb Oct 17 '16 at 14:03
  • In that case you can maybe refer to following: http://www.codeproject.com/KB/cs/svcmgr.aspx?display=Print – Ozan Gunceler Oct 17 '16 at 14:05
  • Tried that solution too. No success. Although I couldn't figure out the domain thing in the parameters though. @Ozan – Tarek Saazeb Oct 17 '16 at 14:08
  • Is the remote machine in a domain? Also is the service is on a domain user account or local account? If user is a local account then you should provide the domain parameter as the network name of the server – Ozan Gunceler Oct 17 '16 at 14:10
  • The remote machine doesn't have any domain info in the network properties. Only group info (which is workgroup). The account I have in there has administrative privilege. Also note, it is not a default installed windows service but a custom one. I developed that service myself. @Ozan – Tarek Saazeb Oct 17 '16 at 14:12
  • Have you checked this? http://stackoverflow.com/a/6912610/4848251 – Ozan Gunceler Oct 17 '16 at 14:14
  • Yes, like I said, I don't have anything to pass for the argument 'domain'. @Ozan – Tarek Saazeb Oct 17 '16 at 14:16
  • If you have access to remote machine, open command prompt and type SET then find the key LOGONSERVER. Try to pass that value (without backslashes) as the domain input parameter – Ozan Gunceler Oct 17 '16 at 14:20
  • Great. SET discovered the USERDOMAIN info. I passed it through the code-project solution, and it is giving me "Cannot open on computer " :( @Ozan – Tarek Saazeb Oct 17 '16 at 14:26
  • We are progressing :) Are you providing displayed name of the service? – Ozan Gunceler Oct 17 '16 at 14:27
  • Displayed name. But it also comes with a space character so I used double quote with the service name. @Ozan – Tarek Saazeb Oct 17 '16 at 14:30
  • On remote machine, click Start > Run and type services.msc Find your custom service and right click on it, choose Properties. Get the value at the top of the window for Service Name which should be different than the display name. Try passing that info – Ozan Gunceler Oct 17 '16 at 14:32
  • Check the screenshot. The display name also has a space – Tarek Saazeb Oct 17 '16 at 14:35
  • http://imgur.com/a/BIv0I – Tarek Saazeb Oct 17 '16 at 14:35
  • Let me try this... I'll get back to you – Ozan Gunceler Oct 17 '16 at 14:36
  • After starting impersonation, can you run the following and check if you see your service in the list ? ServiceController[] _Services = ServiceController.GetServices("IP or NetworkName"); foreach (var service in _Services) { Console.WriteLine("Display name : " + service.DisplayName); } – Ozan Gunceler Oct 17 '16 at 14:49
  • It gave me "Cannot open Service Control Manager on Computer . This operation might require other privileges" – Tarek Saazeb Oct 17 '16 at 15:21