public void UndoImpersonationTest() { var target = new WindowsSecurityHelper(); target.UndoImpersonation(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
/// <summary> /// Performs taks main actions. /// </summary> /// <param name="timer"></param> protected override void OnExecute(Timer timer) { using (var securityHelper = new WindowsSecurityHelper()) { STrace.Trace(GetType().FullName, "Checking services status."); if (!securityHelper.ImpersonateValidUser()) { throw new Exception("Can not impersonate admin user for restarting a service."); } var crashedServices = GetCrasehdServices(); if (!crashedServices.Any()) { STrace.Trace(GetType().FullName, "All services are running normally."); } else { foreach (var winService in crashedServices) { RestartService(winService); } } securityHelper.UndoImpersonation(); } }
/// <summary> /// Change service status. /// </summary> /// <param name="item"></param> private void ServiceReact(string item) { using (var securityHelper = new WindowsSecurityHelper()) { if (!securityHelper.ImpersonateValidUser()) { ThrowError("IMPERSONATION_FAILED"); } var service = new ServiceController(item); switch (service.Status) { case ServiceControllerStatus.Running: service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); break; case ServiceControllerStatus.StopPending: //Procesos string query = string.Format("SELECT ProcessId FROM Win32_Service WHERE Name='{0}'", service.ServiceName); var searcher = new ManagementObjectSearcher(query); foreach (ManagementObject obj in searcher.Get()) { var id = ((int)obj["ProcessId"]); var process = System.Diagnostics.Process.GetProcessById(id); process.Kill(); } break; case ServiceControllerStatus.Stopped: service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); break; } Bind(); securityHelper.UndoImpersonation(); } }