简体   繁体   English

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

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

I apologies first as it might seem duplicate question. 我首先道歉,因为它似乎是重复的问题。 But I did a lot google even I reached two very similar one like: 但是即使我遇到了两个非常相似的Google,我也做了很多事情:

  1. How to remotely control a Windows Service with ServiceController? 如何使用ServiceController远程控制Windows服务?

But unfortunately none of them is working for me. 但不幸的是,他们都没有为我工作。

I have developed a windows service which runs on a windows server 2008 R2 standard machine. 我已经开发了在Windows Server 2008 R2标准计算机上运行的Windows服务。

Service is running very well and working nice as it should work. 服务运行得很好,并且可以正常工作。

My issue is I want to make a desktop application which will run on our local network. 我的问题是我想制作一个可以在我们的本地网络上运行的桌面应用程序。 From this application I want to do some basic operation like getting getting service status, stopping and restarting. 从这个应用程序中,我想执行一些基本操作,例如获取服务状态,停止和重新启动。

Here is my work around. 这是我的工作。

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."}
}

Please let me know am I doing some mistake? 请让我知道我做错了吗? How should I achieve my goal? 我应该如何实现我的目标?

NB: My development PC is Windows 7 Ultimate where as service is laying on Windows Server 2008 R2 Standard. 注意:我的开发PC是Windows 7 Ultimate,该服务基于Windows Server 2008 R2 Standard。 Service is Running under Network Service. 服务正在网络服务下运行。 (I changed it to Administrator logon as well but no luck) (我也将其更改为管理员登录,但没有运气)

Thanks 谢谢

Try this code, its uses your ManagementScope, but queries the service using a ManagementObjectSearcher form the link I provided in the comments. 尝试使用此代码,它使用您的ManagementScope,但是使用ManagementObjectSearcher通过我在注释中提供的链接来查询服务。

If its not that I'd be looking into whether your user has rights to do what you need. 如果不是这样,我将调查您的用户是否有权执行您需要的操作。

    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.

相关问题 从服务器到本地网络中的远程计算机的弹出对话框 - Popup dialog from server to remote computer in local network 如何将文件从本地计算机复制到远程计算机? - How to copy file from a local computer to a remote computer? 如何从Windows服务关闭远程计算机 - How to shutdown a remote computer from a windows service 如何从 LAN 网络计算机访问已注册的 ActiveX 控件? - how to access the registered ActiveX control from the LAN network computer? 如何从远程 Windows 计算机检索机器名称? - How can I retrieve the machine name from a remote Windows computer? 如何从远程计算机访问 Windows 11 上的 IIS Express 站点 - How to access an IIS Express site on Windows 11 from a remote computer C#如何从本地网络检查远程IP地址 - C# How to check Remote IP Address from the Local Network 从远程计算机调用Windows服务 - Call A Windows Service from a remote computer 如何检查是否在远程计算机上启用了Windows更新 - How to check if windows updates are enabled in a remote computer 如何使用.Net Core从远程计算机,同一网络,linux获取mac-address - How to get mac-address from a remote computer, same network, at linux, using .Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM