none
how to stop windows service remotely that located same network RRS feed

  • Question

  • hi ;

    I reviewed some article But I can't do it ;

    how to stop windows service remotely that located same network

    • Moved by Sabah ShariqMVP Monday, August 28, 2017 4:39 AM Moved From Visual C#
    Monday, August 28, 2017 12:57 AM

Answers

  • of course , But I must know the username and password for checking remote service controller,

    I asked for , I want to check the service that worked server from client,

    So it was easy to do via IIS or WCF .

    • Marked as answer by Aly14 Wednesday, August 30, 2017 1:25 PM
    Wednesday, August 30, 2017 1:25 PM

All replies

  • Make sure your machine have access to TCP 135 of target machine (check firewall rules), and check your current account is local "Administrators" group members on that machine. (You can also just grant start/stop access to that service if "subinacl" command is available)


    • Edited by cheong00 Monday, August 28, 2017 2:57 AM
    Monday, August 28, 2017 2:57 AM
  • hi ;

    I reviewed some article But I can't do it ;

    how to stop windows service remotely that located same network

    But red your problem is in the Visual C # area inside, do not understand you that is only in the Windows settings, or only through the C #?


    Monday, August 28, 2017 4:17 AM
  • Hi Aly14,

    You could use the command line to achieve it, like this:

    sc \\remotemachinename stop <relatedservicename>

    If you want to stop remote service via C#, please refer to the following code.

    try
                    {
    
                        #region Code to start the service
                   
                        string serviceName = "Service1";
                        string IP="Remote machine IP";
                        string username ="UserID";
                        string password ="Pass";
    
                        ConnectionOptions connectoptions = new ConnectionOptions();
                        //connectoptions.Impersonation = ImpersonationLevel.Impersonate; 
                        connectoptions.Username = username;
                        connectoptions.Password = pass;
    
                        //IP Address of the remote machine
                      
                        ManagementScope scope = new ManagementScope(@"\\" + IP + @"\root\cimv2");
                        scope.Options = connectoptions;
    
                        //WMI query to be executed on the remote machine
                        SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");
    
                        using (ManagementObjectSearcher searcher = new
                                    ManagementObjectSearcher(scope, query))
                        {
                            ManagementObjectCollection collection = searcher.Get();
                            foreach (ManagementObject service in collection)
                            {
    
    if (service["Started"].Equals(true))
                            {
                                //Stop the service
                                service.InvokeMethod("StopService", null);                            
                            } 
                  //if (service["Started"].Equals(false))
                    //{
                       //Start the service
                       //service.InvokeMethod("StartService", null);                              
                    //}
    
                            }
                        }
                       
               #endregion
    
                    }
                    catch ()
                    {
                      
                    }
    

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 28, 2017 6:14 AM
  • I decided to do it via WCF due to the project also included web project .

    Thanks in advance .

    Monday, August 28, 2017 2:21 PM
  • I think there shouldn't be problem to do it at WCF endpoints.

    It also mean you can just grant the start/stop right to that service for the account that runs WCF locally, so you can just start/stop the service via normal ServiceController way.

    Tuesday, August 29, 2017 1:24 AM
  • of course , But I must know the username and password for checking remote service controller,

    I asked for , I want to check the service that worked server from client,

    So it was easy to do via IIS or WCF .

    • Marked as answer by Aly14 Wednesday, August 30, 2017 1:25 PM
    Wednesday, August 30, 2017 1:25 PM
  • With ServiceController, as long as you've grant right for your worker process account to Start/Stop the service, you can start/stop the service without the need of username and password, because that is the account your code already runs on.
    • Edited by cheong00 Wednesday, August 30, 2017 3:42 PM
    Wednesday, August 30, 2017 3:41 PM
  • The service is running on Server side , The application is running in client side ,all side is located same network , in this case ,Can I check ?

    I can't test due to dont have a test enviorement .

    Wednesday, August 30, 2017 5:19 PM
  • Even in that case, as long as:

    1) both machines are in the same domain,

    2) the "Domain Users" group is a member of local "Users" group of target machine,

    3) the "Domain Users" group has given right to start/stop that desired service,

    4) the firewall of target machine is properly configured to accept incoming connection of TCP port 135

    You should be able to do that without problem.

    But from safety perspective, since you're already using WCF, it'd be much safer to just host a WCF service on that machine responsible to start/stop of that windows service.

    Wednesday, August 30, 2017 11:08 PM
  • both machine in same network But We are not use domain ,we are working as workgroup .
    Thursday, August 31, 2017 8:56 AM