繁体   English   中英

如何远程启动和停止服务

[英]How can i start and stop a service remotely

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

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

我收到错误:System.InvalidOperationException: Der Dienst TeamViewer kann nicht auf dem Computer MYSERVERNAME geöffnet werden。 ---> System.ComponentModel.Win32Exception: Zugriff verweigert,这显然与用户权限有关。 所以我在谷歌上搜索了很长一段时间关于模拟和 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);
    }
}

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

ps:我的 Powershell 脚本运行起来很有魅力,但我想让它看起来更复杂 :)

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

我找到了一个帖子,它帮助我完成了我的代码。 可以在这里找到。

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

            }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM