如何远程启动和停止服务

时间:2016-09-01 13:36:26

标签: c#

这是我第一次在这里发帖和第二次编码。因此,为了了解这一切是如何工作的,我尝试查找代码片段并复制/粘贴它们,直到我的应用程序似乎运行。我之前写了一些批处理和ps脚本,但老实说,我更喜欢系统管理和硬件....直到现在!

我的项目是一个简单的GUI工具,可以在某个服务器上启动和停止TeamViewer服务。我希望尽可能简单地保持它,直到我在同事计算机上启动应用程序以向他们展示如何使用它之前,它似乎有效。

我收到错误:System.InvalidOperationException:Der Dienst TeamViewer kann nicht auf dem ComputerMYSERVERNAMEgeöffnetwerden。 ---> System.ComponentModel.Win32Exception:Zugriff verweigert,显然与用户权限有关。所以我用Google搜索了很多关于模拟和WMI服务凭据的信息,但现在我被困住了,不得不请求你们帮忙。

所以这是我的代码:

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);
    }
}

如果有人能帮助我,我会很高兴的!

p.s。:我的Powershell Scripts就像一个魅力,但我想让它看起来更复杂:)

Edit1:我尝试停止/启动服务的服务器不是域的成员,但域的每个成员都应该能够停止/启动服务。

1 个答案:

答案 0 :(得分:4)

我发现了一条帖子,帮助我完成了我的代码。它可以找到here

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

            }
    }