protected override void ProcessRecord()
        {
            List <ServiceSummary> scList = new List <ServiceSummary>();

            foreach (ServiceController sc in ServiceControl.GetServiceController(
                         Name, IgnoreServiceName, IgnoreDisplayName))
            {
                scList.Add(new ServiceSummary(sc));
                if (sc.Status == ServiceControllerStatus.Running && sc.CanStop)
                {
                    sc.Stop();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped);
                    sc.Start();
                    if (!RunAsync)
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running);
                    }
                }
                else if (sc.Status == ServiceControllerStatus.Stopped)
                {
                    sc.Start();
                    if (!RunAsync)
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running);
                    }
                }
            }
            WriteObject(scList);
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            List <ServiceSummary> scList = new List <ServiceSummary>(
                ServiceControl.GetServiceController(Name, IgnoreServiceName, IgnoreDisplayName).
                Select(x => new ServiceSummary(x)));

            WriteObject(scList);
        }
Beispiel #3
0
        protected override void ProcessRecord()
        {
            /*
             * ServiceController[] scs = null;
             * if (Name.Contains("*"))
             * {
             *  foreach (ServiceController sc in ServiceController.GetServices())
             *  {
             *      if(sc.ServiceName)
             *  }
             * }
             * else
             * {
             *  scs = new ServiceController[] { ServiceControl.GetServiceController(Name) };
             * }
             * ServiceController serviceController = ServiceControl.GetServiceController(Name);
             */

            foreach (ServiceController serviceController in ServiceControl.GetServiceController(
                         Name, IgnoreServiceName, IgnoreDisplayName))
            {
                IntPtr serviceManagerHandle = OpenServiceManagerHandle();
                IntPtr serviceHandle        = OpenServiceHandle(serviceController, serviceManagerHandle);

                try
                {
                    if (StartupType == DELAYED_AUTOMATIC)
                    {
                        ChangeServiceStartType(serviceHandle, AUTOMATIC);
                        ChangeDelayedAutoStart(serviceHandle, true);
                    }
                    else
                    {
                        ChangeDelayedAutoStart(serviceHandle, false);
                        ChangeServiceStartType(serviceHandle, StartupType);
                    }
                }
                catch { }
                if (serviceHandle != IntPtr.Zero)
                {
                    CloseServiceHandle(serviceHandle);
                }
                if (serviceHandle != IntPtr.Zero)
                {
                    CloseServiceHandle(serviceManagerHandle);
                }
            }
        }