How to remotely control a Windows Service with ServiceController?
Asked Answered
V

4

21

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.

Vanscoy answered 8/6, 2010 at 9:54 Comment(0)
V
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!

Vanscoy answered 8/6, 2010 at 13:10 Comment(2)
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.Lemur
@Lemur 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.Millipede
D
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

Distress answered 8/6, 2010 at 13:12 Comment(0)
N
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

Nonconcurrence answered 22/10, 2013 at 11:21 Comment(0)
W
0

i had same issue but is done. try this code bellow

 protected void Button4_Click1(object sender, EventArgs e)
    {
        //1º connect to remote computer
        ConnectionOptions connection = new ConnectionOptions();
        connection.Username = "USER NAME OF REMOTE COMPUTER";
        connection.Password = "PASS WORD OF REMOTE COMPUTER";
        connection.Authority = "NTLMDOMAIN:DOMINE NAME OF YOUR LAN";
        connection.EnablePrivileges = true;
        connection.Authentication = AuthenticationLevel.Default;
        connection.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(
            "\\\\NAME OR IP OF REMOTE COMPUTER\\root\\CIMV2", connection);
        scope.Connect();
         // finsh connection
      
        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
        moSearcher.Scope = scope;
        moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='SERVICE NAME IN REMOTE COMPUTER'");
        ManagementObjectCollection mbCollection = moSearcher.Get();

      // ServiceController svc = new ServiceController("Jenkins", "10.224.62.35");

      //namelbl.Text = svc.Status.ToString();

        foreach (ManagementObject oReturn in mbCollection)
        {
       // invoke start
        ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);

        //invoke stop
        ManagementBaseObject outParams2 = oReturn.InvokeMethod("StopService", null, null);

        //get result
        string a = outParams["ReturnValue"].ToString();

       // get service state
         string state = oReturn.Properties["State"].Value.ToString().Trim();

        MessageBox.Show(state);// TO DISPLAY STATOS FROM SERVICE IN REMOTE COMPUTER
        }


       
        

    }//THE CODE ABOVE IS WRITER IN C#  I HOPE HELP SOME ONE. THANKS
Waterworks answered 27/12, 2022 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.