bool WindowsStop() { ImpersonateUser iu = new ImpersonateUser(); try { if (!(string.IsNullOrEmpty(this.Username) || string.IsNullOrEmpty(this.Password))) { iu.Impersonate(this.Domain, this.Username, AES.DecryptString(this.Password)); } using (ServiceController sc = new ServiceController(this.Value, this.Host)) { OnLog(new LogEventArgs(string.Format("Stopping {0} on {1}", this.Name, this.Server))); if (sc.Status == ServiceControllerStatus.Stopped) { OnLog(new LogEventArgs(string.Format("Service {0} already stopped on {1}", this.Name, this.Server))); OnProgress(new EventArgs()); return(true); } sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, this.TimeOut)); sc.Refresh(); OnLog(new LogEventArgs(string.Format("[{2}] - {0} - {1}", this.Name, sc.Status, this.Server))); OnProgress(new EventArgs()); Thread.Sleep(this.StopDelay * 1000); return(sc.Status == ServiceControllerStatus.Stopped); } } finally { iu.Undo(); } }
public void GetServices(BO.Host host) { host.Services = new List <BO.Service>(); try { ImpersonateUser iu = new ImpersonateUser(); if (!(string.IsNullOrEmpty(host.Username) || string.IsNullOrEmpty(host.Password))) { iu.Impersonate(host.Domain, host.Username, AES.DecryptString(host.Password)); } System.ServiceProcess.ServiceController[] sc = System.ServiceProcess.ServiceController.GetServices(host.Value); foreach (var s in sc) { host.Services.Add(new BO.Service { Name = s.DisplayName, Value = s.ServiceName, Status = s.Status }); } iu.Undo(); } catch (Exception) { //OnLog(new BO.LogEventArgs(string.Format("Failed to Connect to Server: {0} [{1}]\r\n", host.Name, host.Value ))); } }
private void EndImpersonation() { if (ImpersonationHandler.IsNull()) { return; } ImpersonationHandler.Undo(); ImpersonationHandler.TryDispose(); ImpersonationHandler = null; }