简体   繁体   English

如何用ServiceController远程控制一个Windows服务?

[英]How to remotely control a Windows Service with ServiceController?

I'm trying to control Windows Services that are installed in a remote computer.我正在尝试控制安装在远程计算机上的 Windows 服务。 I'm using the ServiceController class.我正在使用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:有了这个,我可以像这样获得 Windows 服务的状态:

string status = svc.Status.ToString();

But I can't control the Windows Service (by doing svc.Start(); or svc.Stop(); ).但我无法控制 Windows 服务(通过执行svc.Start();svc.Stop(); )。 I get the following exception:我得到以下异常:

Cannot open Servicexxx service on computer 'COMPUTER_NAME'无法在计算机“COMPUTER_NAME”上打开 Servicexxx 服务

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.注意:本地和远程计算机都运行 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! 我找到了这个非常有用的项目: http//www.codeproject.com/KB/cs/svcmgr.aspx? display =打印给我很多帮助!

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 在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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