20

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.

Luke Girvin
  • 12,913
  • 8
  • 60
  • 81
Amokrane Chentir
  • 29,280
  • 37
  • 112
  • 157

3 Answers3

11

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!

Amokrane Chentir
  • 29,280
  • 37
  • 112
  • 157
  • 3
    This is an unnecessary security hole. If the user of this application should have the access then they should have the credentials and login as that user. More importantly they should just have the permissions assigned to a group they are a member of. This doesn't solve the problem it's an insecure workaround See Hans Passant's answer and the associated comments. – majinnaibu Apr 02 '15 at 18:01
  • @majinnaibu As a tester for an organization, it's necessary for us to use a Service Account defined in the Active Directory and impersonation to test a windows service remotely. – Su Llewellyn Mar 22 '18 at 16:03
8

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

Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455
0

In order to solve the issue , give your name the admin permissions on remote computer/server like domain/username and you will able to run the package successfully since i had the same issue and when i gave permissions to my self services were accessible on remote server

GOOKI
  • 1