How can I start and stop the service remotely

This is my first post here and my second time encoding. Therefore, in order to find out how all this works, I try to find code fragments and copy / paste them until my application is launched. I used to write some batch and ps scripts, but to be honest, I'm more in system administration and hardware ... so far!

My project is a simple GUI tool to start and stop the TeamViewer service on a specific server. I wanted to keep it as simple as possible, and it seemed to work until I ran the application on my colleagues' computers to show them how to use it.

I get an error: System.InvalidOperationException: Der Dienst TeamViewer kann nicht auf dem Computer MYSERVERNAME geöffnet werden. ---> System.ComponentModel.Win32Exception: Zugriff verweigert, which is obviously related to user privileges. So I have been looking for impersonation and WMI for a long time, but now I'm stuck and should ask you guys for your help.

So here is my code:

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();
    }

    private void EIN_Click(object sender, EventArgs e)
    {
        String svcName = "TeamViewer";
        String machineName = "MYSERVERNAME";
        var sc = new System.ServiceProcess.ServiceController(svcName, machineName);
        sc.Start();
        sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
    }

    private void AUS_Click(object sender, EventArgs e)
    {
        String svcName = "TeamViewer";
        String machineName = "MYSERVERNAME";
        var sc = new System.ServiceProcess.ServiceController(svcName, machineName);
        sc.Stop();
        sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
    }
}

I would be very happy if someone could help me!

ps: My Powershell scripts worked like a charm, but I want to make it more complicated :)

Edit1: the server on which I am trying to stop / start the service is not a member of the domain, but each member of the domain must be able to stop / start the service.

+4
source share
1 answer

, . .

Edit1: :

private void EIN_Click(object sender, EventArgs e)
    {
      try
            {

                #region Code to start the service

                string serviceName = "TeamViewer";
                string IP="actual-IP-address";
                string username ="actual-username";
                string password ="actual-password";

                ConnectionOptions connectoptions = new ConnectionOptions();
                //connectoptions.Impersonation = ImpersonationLevel.Impersonate;
                connectoptions.Username = username;
                connectoptions.Password = password;

                //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(false))
                        {
                            //Start the service
                            service.InvokeMethod("StartService", null);
                            //here i added a picture box which shows a green button when the service is started
                            pictureBox1.Image = Properties.Resources._120px_Green_Light_I_svg;
                      }

                    }
                }

       #endregion

            }
            catch (NullReferenceException)
            {

            }
    }
+4

Source: https://habr.com/ru/post/1653315/


All Articles