itrytocode
itrytocode

Reputation: 141

How can i start and stop a service remotely

this is my first time posting here and my second time coding. So in order to learn how all this works, i try to look up code snippets and copy/paste them until my application seems to run. I have written some batch and ps scripts before but to be honest, i'm more into system administration and hardware....until now!

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

I get the error: System.InvalidOperationException: Der Dienst TeamViewer kann nicht auf dem Computer MYSERVERNAME geöffnet werden. ---> System.ComponentModel.Win32Exception: Zugriff verweigert, which obviously has to do with user privileges. So i googled for quite a while about Impersonation and WMI service credentials but now i'm stuck and have to ask you guys for your help.

So here's 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'd be very happy if anybody could help me out!

p.s.: My Powershell Scripts worked like a charm but i want to make it look more sophisticated :)

Edit1: The server where i try to stop/start the service is not a member of the domain but every member of the domain should be able to stop/start the service.

Upvotes: 7

Views: 7494

Answers (1)

itrytocode
itrytocode

Reputation: 141

i found a post, that helped me completing my code. It can be found here.

Edit1: Here's how the code looks like now:

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)
            {

            }
    }

Upvotes: 7

Related Questions