如何使用ServiceController远程控制Windows服务?

发布于 2024-09-04 21:31:55 字数 587 浏览 16 评论 0原文

我正在尝试控制安装在远程计算机上的 Windows 服务。我正在使用 ServiceController 类。

我有这个:

ServiceController svc =  new ServiceController("MyWindowsService", "COMPUTER_NAME");

有了这个,我可以像这样获取 Windows 服务的状态:

string status = svc.Status.ToString();

但我无法控制 Windows 服务(通过执行 svc.Start();svc.停止();)。 我得到以下异常:

无法打开 Servicexxx 服务 计算机“COMPUTER_NAME”

这很正常,我想这与访问权限有关。但如何呢? 我查过谷歌,但没有找到我要找的东西。不过我经常读到一些与冒充相关的内容,但我不知道这意味着什么。

注意:本地和远程计算机都运行 Win XP Pro。

I'm trying to control Windows Services that are installed in a remote computer. I'm using the ServiceController class.

I have this:

ServiceController svc =  new ServiceController("MyWindowsService", "COMPUTER_NAME");

With this, I can get the status of the Windows Service like this:

string status = svc.Status.ToString();

But I can't control the Windows Service (by doing svc.Start(); or svc.Stop();).
I get the following exception:

Cannot open Servicexxx service on
computer 'COMPUTER_NAME'

That's normal, I suppose there is something to do with access permissions. But how?
I've looked into Google but didn't find what I was looking for. However I often read something related to impersonation, but I don't know what that means.

NB: The local and remote computers are both running Win XP Pro.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有伤那来痛 2024-09-11 21:31:55

问题解决了。

模拟包括使用特定的登录名/密码运行一段代码。我发现这个非常有用的项目: http://www.codeproject.com/ KB/cs/svcmgr.aspx?display=Print 对我帮助很大!

Problem solved.

Impersonation consists in running a piece of code using a certain logon/password. I found this very useful project: http://www.codeproject.com/KB/cs/svcmgr.aspx?display=Print that helped me a lot!

终弃我 2024-09-11 21:31:55

启动和停止服务是一项高度特权的操作,通常只有管理员才能使用。确保您使用的用户帐户在目标计算机上具有足够的权限。在 serverfault.com 上询问更多相关问题

Starting and stopping services is a highly privileged operation, normally available only to administrators. Ensure that the user account you use has sufficient privileges on the target machine. Ask more questions about it at serverfault.com

马蹄踏│碎落叶 2024-09-11 21:31:55

为了解决这个问题,请为您的名字提供远程计算机/服务器上的管理员权限(例如域/用户名),您将能够成功运行该包,因为我遇到了同样的问题,并且当我向我的自助服务授予权限时可以在远程访问服务器

In order to solve the issue , give your name the admin permissions on remote computer/server like domain/username and you will able to run the package successfully since i had the same issue and when i gave permissions to my self services were accessible on remote server

南街女流氓 2024-09-11 21:31:55

我有同样的问题
但已经完成了。
尝试下面的代码

 protected void Button4_Click1(object sender, EventArgs e)
    {
        //1º connect to remote computer
        ConnectionOptions connection = new ConnectionOptions();
        connection.Username = "USER NAME OF REMOTE COMPUTER";
        connection.Password = "PASS WORD OF REMOTE COMPUTER";
        connection.Authority = "NTLMDOMAIN:DOMINE NAME OF YOUR LAN";
        connection.EnablePrivileges = true;
        connection.Authentication = AuthenticationLevel.Default;
        connection.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(
            "\\\\NAME OR IP OF REMOTE COMPUTER\\root\\CIMV2", connection);
        scope.Connect();
         // finsh connection
      
        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
        moSearcher.Scope = scope;
        moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='SERVICE NAME IN REMOTE COMPUTER'");
        ManagementObjectCollection mbCollection = moSearcher.Get();

      // ServiceController svc = new ServiceController("Jenkins", "10.224.62.35");

      //namelbl.Text = svc.Status.ToString();

        foreach (ManagementObject oReturn in mbCollection)
        {
       // invoke start
        ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);

        //invoke stop
        ManagementBaseObject outParams2 = oReturn.InvokeMethod("StopService", null, null);

        //get result
        string a = outParams["ReturnValue"].ToString();

       // get service state
         string state = oReturn.Properties["State"].Value.ToString().Trim();

        MessageBox.Show(state);// TO DISPLAY STATOS FROM SERVICE IN REMOTE COMPUTER
        }


       
        

    }//THE CODE ABOVE IS WRITER IN C#  I HOPE HELP SOME ONE. THANKS

i had same issue
but is done.
try this code bellow

 protected void Button4_Click1(object sender, EventArgs e)
    {
        //1º connect to remote computer
        ConnectionOptions connection = new ConnectionOptions();
        connection.Username = "USER NAME OF REMOTE COMPUTER";
        connection.Password = "PASS WORD OF REMOTE COMPUTER";
        connection.Authority = "NTLMDOMAIN:DOMINE NAME OF YOUR LAN";
        connection.EnablePrivileges = true;
        connection.Authentication = AuthenticationLevel.Default;
        connection.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(
            "\\\\NAME OR IP OF REMOTE COMPUTER\\root\\CIMV2", connection);
        scope.Connect();
         // finsh connection
      
        ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
        moSearcher.Scope = scope;
        moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='SERVICE NAME IN REMOTE COMPUTER'");
        ManagementObjectCollection mbCollection = moSearcher.Get();

      // ServiceController svc = new ServiceController("Jenkins", "10.224.62.35");

      //namelbl.Text = svc.Status.ToString();

        foreach (ManagementObject oReturn in mbCollection)
        {
       // invoke start
        ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);

        //invoke stop
        ManagementBaseObject outParams2 = oReturn.InvokeMethod("StopService", null, null);

        //get result
        string a = outParams["ReturnValue"].ToString();

       // get service state
         string state = oReturn.Properties["State"].Value.ToString().Trim();

        MessageBox.Show(state);// TO DISPLAY STATOS FROM SERVICE IN REMOTE COMPUTER
        }


       
        

    }//THE CODE ABOVE IS WRITER IN C#  I HOPE HELP SOME ONE. THANKS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文