Here is a simple method to start a service on a remote system using C#.
Call the method using
1 |
string status = servicestart("computerName", "Service"); |
Method will attempt to change the status and return the current Status
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private string servicestart(string compname, string service) { ServiceController sc = new ServiceController(); sc.ServiceName = service; sc.MachineName = compname; string result = sc.Status.ToString(); if (sc.Status == ServiceControllerStatus.Stopped) { try { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); result = sc.Status.ToString(); } catch (InvalidOperationException) { // do something } } return result; } |