开发者

Start A Remote Service From C#

I'm using the following code in a C# WinForms app to start windows services on a remote PC

    public static List<Service> GetServices()
    {
        List<Service> Services = new List<Service>();
        ServiceController[] sc = ServiceController.GetServices(Server);
        foreach (var s in sc)
        {
           Services.Add(new Service { Name = s.ServiceName, Running = s.Status == ServiceControllerStatus.Running });
        }
        return Services;
    }

    public static bool StartService(string ServiceName)
    {
        try
        {
            ServiceController sc = new ServiceController(ServiceName, Server);
            sc.Start();
            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
            sc.Refresh();
            return sc.Status == ServiceControllerStatus.Running;
        }
        catch(Exception ex) { return false; }
    }

The GetServices method works fine when pointing at my local PC or at the remote PC. The StartService method however only works on my local PC, when run on the remote PC I get access denied. In this cas开发者_StackOverflowe the remote PC is a windows XP pro machine on the same domain and the user I'm running the app under has local admin rights on it.

I'm not sure if this is an issue with my code or if my permissions are not correct.

If this is a permissions issue please let me know and I'll try asking on ServerFault.

Thanks


If it turns out, you just want permission to start and stop the window services instead of having Admin rights you'll can modify the service's DACL.

There are basically two ways to do this.

1) Execution of security descriptor definition language (SDDL) string.

2) Inherit from NativeObjectSecurity and apply your DACL changes during ServiceInstaller_AfterInstall method.


It turns out this was a permissions issue. I was testing with a virtual machine and our VM's have different access permissions on our domain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜