views:

88

answers:

2

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 case 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

A: 

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

There are basically two ways to do this.

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

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

Conrad Frix
A: 

It turns out this was a permissions issue. Because I was testing with a virtual machine and the virtual machines has different access permissions on our domain.

Gavin Draper