0

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

Community
  • 1
  • 1
Iqbal
  • 1,266
  • 1
  • 17
  • 21
  • are you on a domain? is Administrator a local user on your server? could a firewall be blocking? – ajg Aug 19 '14 at 10:05
  • @ajg no its not a domain. Administrator is admin on server. I also opened the firewall but no luck. Yes I do notice when firewall turned off that command `service.Status.ToString()` runs very quickly but same result. – Iqbal Aug 19 '14 at 10:09
  • What is IQ-HOME? Have you tried without the options.Authority line? – ajg Aug 19 '14 at 10:19
  • @ajg IQ-HOME is Workgroup. Yes I tried without this option as well. – Iqbal Aug 19 '14 at 10:22
  • try the answer here http://stackoverflow.com/questions/7117877/how-to-access-romote-windows-service-with-related-objectquery using the ManagementScope you build above. – ajg Aug 19 '14 at 10:46
  • also I would expect options.Authority to use the server name not workgroup - so options.Authority = "NTLMDOMAIN:RTOKEN-SERVER"; – ajg Aug 19 '14 at 10:48
  • @ajg I changed Authority to NTLMDOMAIN:RTOKEN-SERVER, still same error. – Iqbal Aug 19 '14 at 10:52

1 Answers1

2

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);
        }
    }
ajg
  • 1,743
  • 12
  • 14
  • Thanks a lot @ajg it works. Can you help me how to restart the service? – Iqbal Aug 19 '14 at 11:39
  • Excellent! The lines to start and stop are already there - just commented out. To restart you stop and then start - both methods should be synchronous I think – ajg Aug 19 '14 at 11:43
  • Sorry I did not notice the commented codes. Its working fine. Thanks @ajg. – Iqbal Aug 19 '14 at 12:02