Exemplo n.º 1
0
        public static void GetServiceList()
        {
            var services = ServiceController.GetServices().OrderBy(u => u.DisplayName).ToList();

            foreach (var srv in services)
            {
                WFHLog.WriteLog($"{srv.DisplayName.Replace(",","")},{srv.ServiceName.Replace(",", "")},{srv.Status.ToString().Replace(",","")},{srv.StartType.ToString().Replace(",","")}, {srv.ServiceType.ToString().Replace(",", "")}   ");
            }
        }
Exemplo n.º 2
0
        public static void StopService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);

            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
            catch (Exception ex)
            {
                WFHLog.WriteLog(serviceName, ex.Message);
            }
        }
Exemplo n.º 3
0
        public static void RestartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);

            try
            {
                int      millisec1 = Environment.TickCount;
                TimeSpan timeout   = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                // count the rest of the timeout
                int millisec2 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch (Exception ex)
            {
                WFHLog.WriteLog(serviceName, ex.Message);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string[]      lines            = File.ReadAllLines(WFHCommonHelpers.ServiceCSVFilePath);
            List <string> serviceNotFound  = new List <string>();
            List <string> serviceException = new List <string>();
            List <string> serviceExecuted  = new List <string>();
            List <string> serviceNoChange  = new List <string>();


            WFHServices.GetServiceList();


            foreach (string line in lines)
            {
                string[] csv = line.Split(',');
                if (csv.Length > 1)
                {
                    string           serviceName        = csv[1];
                    string           serviceDescription = csv[0];
                    string           serviceStartupMode = csv[2];
                    ServiceStartMode mode;
                    bool             IsServiceExists = WFHServices.IsServiceExists(serviceName, out mode);
                    if (IsServiceExists)
                    {
                        try
                        {
                            if (Enum.TryParse(serviceStartupMode, out ServiceStartMode serviceStartupModeEnum))
                            {
                                if (mode == serviceStartupModeEnum)
                                {
                                    serviceNoChange.Add($"Service:{serviceName}");
                                }
                                else
                                {
                                    WFHServices.ChangeStartMode(serviceName, serviceStartupModeEnum);
                                    serviceExecuted.Add($"Service: { serviceName}");
                                }
                            }
                            else
                            {
                                serviceException.Add($"Service: {serviceName} Enum Not Found");
                            }
                        }
                        catch (Exception ex)
                        {
                            serviceException.Add($"Service: {serviceName},{serviceDescription} with expection {ex.Message}");
                        }
                    }
                    else
                    {
                        serviceNotFound.Add($"Service: {serviceName}");
                    }
                }
            }


            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            WFHLog.WriteLog("-------------------------------");
            WFHLog.WriteLog($"Total Services {serviceNoChange.Count} No Change");
            WFHLog.WriteLog("-------------------------------");

            foreach (string s in serviceNoChange)
            {
                WFHLog.WriteLog(s);
            }

            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            WFHLog.WriteLog("-------------------------------");
            WFHLog.WriteLog($"Total Services {serviceExecuted.Count} Executed");
            WFHLog.WriteLog("-------------------------------");

            foreach (string s in serviceExecuted)
            {
                WFHLog.WriteLog(s);
            }

            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            WFHLog.WriteLog("-------------------------------");
            WFHLog.WriteLog($"Total Services {serviceNotFound.Count} Does Not Exists");
            WFHLog.WriteLog("-------------------------------");

            foreach (string s in serviceNotFound)
            {
                WFHLog.WriteLog(s);
            }

            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            WFHLog.WriteLog("-------------------------------");
            WFHLog.WriteLog($"Total Services  {serviceException.Count} With Exceptions");
            WFHLog.WriteLog("-------------------------------");
            foreach (string s in serviceException)
            {
                WFHLog.WriteLog(s);
            }

            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            WFHLog.WriteLog("");
            int total = serviceException.Count + serviceExecuted.Count + serviceNoChange.Count + serviceNotFound.Count;

            WFHLog.WriteLog($"Total Processed Service: {total}");
            WFHLog.WriteLog($"Total Provided Service: {lines.Length}");

            WFHServices.GetServiceList();

            WFHLog.ReadMessage();
        }