How can I programmatically stop/start a windows service on a remote box?

后端 未结 9 1706
清歌不尽
清歌不尽 2020-11-27 05:17

I want to write a console or Click Once WinForms app that will programmatically stop and/or start a windows service on a remote box.

Both boxes are running .NET 3.5

相关标签:
9条回答
  • 2020-11-27 05:43

    You can use System.Management APIs (WMI) to control services remotely. WMI is the generic API to do administrative tasks.

    For this problem, however, I suggest you to use the easier to use System.ServiceProcess.ServiceController class.

    0 讨论(0)
  • 2020-11-27 05:44

    galets code snippet above is a great start. However, keep in mind it assumes that the service has already started, or, more importantly, that

    sc.Status == System.ServiceProcess.ServiceControllerStatus.Running
    

    Also, it may important to, at some point during code execution, call

    sc.Refresh();
    

    because the properties values (such as ServiceControllerStatus) may not reflect the actual properties of the service. For instance, you may call

    sc.Start();
    

    and wait indefinitely when this command executes

    sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running)
    

    Here is a version of this code that I coded with those considerations in mind.

                //Restart Content Service on DEV. 
            String svcName = "TheServiceName";
            String machineName = "TheMachineName";
            var sc = new System.ServiceProcess.ServiceController(svcName, machineName);
            Console.WriteLine("Stopping Service '{0}' on machine '{1}", svcName, machineName);
            sc.Stop();
            sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);          
    
            //sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
            do
            {
                try
                {
                    sc.Refresh();
                    if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        Console.WriteLine("Code has detected that servive start is pending, waiting 5 seconds to see if status changes..");
                        System.Threading.Thread.Sleep(5000);
                    }
                    else
                    {
                        Console.WriteLine("waiting 5 seconds and retrying start..");
                        System.Threading.Thread.Sleep(5000);
                        Console.WriteLine("Attempt Starting Service '{0}' on machine '{1}", svcName, machineName);
                        sc.Start();
                    }
                }
    
                catch(Exception ex)
                {
                    //If it is already running, then abort do while
                    if (ex.InnerException.Message == "An instance of the service is already running")
                    {
                        Console.WriteLine(ex.InnerException.Message);
                        continue;
                    }
                    Console.WriteLine(ex.InnerException.ToString());
                }
            } while (sc.Status != System.ServiceProcess.ServiceControllerStatus.Running);
    
    0 讨论(0)
  • 2020-11-27 05:46

    in C#:

    var sc = new System.ServiceProcess.ServiceController("MyService", "MyRemoteMachine");
    sc.Start();
    sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
    sc.Stop();
    sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
    
    0 讨论(0)
提交回复
热议问题