简体   繁体   中英

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:

  1. How to remotely control a Windows Service with ServiceController?

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.

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

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);
        }
    }

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