繁体   English   中英

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

[英]How to control windows services from remote computer in local network?

我首先道歉,因为它似乎是重复的问题。 但是即使我遇到了两个非常相似的Google,我也做了很多事情:

  1. 如何使用ServiceController远程控制Windows服务?

但不幸的是,他们都没有为我工作。

我已经开发了在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 Ultimate,该服务基于Windows Server 2008 R2 Standard。 服务正在网络服务下运行。 (我也将其更改为管理员登录,但没有运气)

谢谢

尝试使用此代码,它使用您的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);
        }
    }

暂无
暂无

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

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