如何从本地网络中的远程计算机控制Windows服务?

时间:2014-08-19 09:51:53

标签: c# windows

我首先道歉,因为它似乎是重复的问题。 但我做了很多谷歌甚至我达到了两个非常相似的像:

  1. How to remotely control a Windows Service with ServiceController?
  2. 但不幸的是,他们中没有人为我工作。

    我开发了一个在Windows Server 2008 R2标准机器上运行的Windows服务。

    服务运行良好,工作得很好,因为它应该有效。

    我的问题是我想创建一个可以在我们的本地网络上运行的桌面应用程序。 从这个应用程序,我想做一些基本的操作,如获取服务状态,停止和重新启动。

    这是我的工作。

    private void WSControllerForm_Load(object sender, System.EventArgs e)
    {
        ConnectionOptions options = new ConnectionOptions();
        options.Password = "password";
        options.Username = "Administrator";
        options.Impersonation =
            System.Management.ImpersonationLevel.Impersonate;
        options.EnablePrivileges = true;
        options.Authority = "NTLMDOMAIN:IQ-HOME";
        options.Authentication = AuthenticationLevel.PacketPrivacy;
    
        ManagementScope scope =
            new ManagementScope(@"\\RTOKEN-SERVER\root\cimv2", options);
    
        scope.Connect();        //checked its connected
    
        // Make a connection to a remote computer. 
        // Replace the "FullComputerName" section of the
        // string "\\\\FullComputerName\\root\\cimv2" with
        // the full computer name or IP address of the 
        // remote computer.
    
        ServiceController service = new ServiceController("Recharger Token", "RTOKEN-SERVER");
        service.Refresh();
        MessageBox.Show(service.Status.ToString()); //Error raised: {"Cannot open Service Control Manager on computer 'rToken-server'. This operation might require other privileges."}
    }
    

    请告诉我,我做错了吗? 我该如何实现目标?

    注意:我的开发PC是Windows 7旗舰版,其服务在Windows Server 2008 R2 Standard上。 服务正在网络服务下运行。 (我将其更改为管理员登录,但没有运气)

    由于

1 个答案:

答案 0 :(得分:1)

尝试使用此代码,它使用您的ManagementScope,但使用ManagementObjectSearcher从我在注释中提供的链接查询服务。

如果我不是在考虑您的用户是否有权做您需要的事情。

    private void WSControllerForm_Load(object sender, System.EventArgs e)
    {
        ConnectionOptions options = new ConnectionOptions();
        options.Password = "password";
        options.Username = "Administrator";

        //i'm not 100% sure these 4 lines are needed - try without if it still fails
        options.Impersonation =
            System.Management.ImpersonationLevel.Impersonate;
        options.EnablePrivileges = true;
        options.Authority = "NTLMDOMAIN:RTOKEN-SERVER";
        options.Authentication = AuthenticationLevel.PacketPrivacy;

        ManagementScope scope =
            new ManagementScope(@"\\RTOKEN-SERVER\root\cimv2", options);

        scope.Connect();

        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
        moSearcher.Scope = scope;
        moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='Recharger Token'");
        ManagementObjectCollection mbCollection = moSearcher.Get();

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

            //invoke stop
            //ManagementBaseObject outParams = 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);
        }
    }