简体   繁体   中英

How to remotely control a Windows Service with ServiceController?

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.

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!

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

为了解决这个问题,请在远程计算机/服务器上为您的名称提供管理员权限,例如域/用户名,并且您可以成功运行该程序包,因为我遇到了同样的问题,并且当我向自助服务授予权限时,可以在远程访问服务器

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM