Exemplo n.º 1
0
        /// <summary>
        /// Create a Windows service when it does not exist, else configure it.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="displayName"></param>
        /// <param name="binPath"></param>
        /// <param name="userName"></param>
        /// <param name="unecryptedPassword"></param>
        /// <param name="startupType"></param>
        /// <param name="type"></param>
        public static void runProcess(string displayName, string binPath, string userName, string unecryptedPassword, ServiceStartUpType startupType,
                                      ServiceAction type)
        {
            // Determine if service has to be created (Create) or edited (Config)
            StringBuilder builder = new StringBuilder();

            switch (type)
            {
            case ServiceAction.create:
                builder.AppendFormat("{0} {1} ", type.ToString(), displayName);
                builder.AppendFormat("binPath= \"{0}\"  ", binPath);
                //builder.AppendFormat("displayName= \"{0}\"  ", displayName);

                // Only add "obj" when username is not empty. If omitted the "Local System" account will be used
                if (!string.IsNullOrEmpty(userName))
                {
                    builder.AppendFormat("obj= \"{0}\"  ", userName);
                }

                // Only add password when unecryptedPassword it is not empty and user name is not "NT AUTHORITY\Local Service" or NT AUTHORITY\NetworkService
                if (!string.IsNullOrEmpty(unecryptedPassword) &&
                    !unecryptedPassword.Equals(@"NT AUTHORITY\Local Service") &&
                    !unecryptedPassword.Equals(@"NT AUTHORITY\NetworkService"))
                {
                    builder.AppendFormat("password= \"{0}\"  ", unecryptedPassword);
                }
                builder.AppendFormat("start= \"{0}\"  ", startupType.ToString());
                break;

            case ServiceAction.Config:
                builder.AppendFormat("config {0} ", displayName);
                builder.AppendFormat("start= \"{0}\"  ", startupType.ToString());
                break;

            case ServiceAction.delete:
                builder.AppendFormat("{0} {1} ", type.ToString(), displayName);
                break;

            case ServiceAction.stop:
                builder.AppendFormat("{0} {1} ", type.ToString(), displayName);
                break;

            case ServiceAction.start:
                builder.AppendFormat("{0} {1} ", type.ToString(), displayName);
                break;
            }
            // Execute sc.exe commando
            using (Process process = new Process())
            {
                process.StartInfo.FileName       = @"sc.exe";
                process.StartInfo.Arguments      = builder.ToString();
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                process.Start();
                process.Close();
            }
        }
    public void ProcessServiceRequest(ServiceAction action, ScheduledTask task, int timeout)
    {
        ScheduledTaskConfig status = null;
        bool success = false;

        switch (action)
        {
        case ServiceAction.Query:
            break;

        case ServiceAction.Start:
            success = ScheduledTaskUtil.Start(task.Name, task.Server);
            break;

        case ServiceAction.Stop:
            success = ScheduledTaskUtil.Stop(task.Name, task.Server);
            break;

        case ServiceAction.Restart:
            success = ScheduledTaskUtil.Stop(task.Name, task.Server);
            System.Threading.Thread.Sleep(5000);
            success = ScheduledTaskUtil.Start(task.Name, task.Server);
            break;
        }

        status = ScheduledTaskUtil.QueryStatus(task.Name, task.Server);

        if (status != null)
        {
            OnLogMessage(action.ToString(), "Name : [" + status.Name + "] Status : [" + status.State + "]");
            results.Add(status);
        }
    }
Exemplo n.º 3
0
        internal override void Run(List <VariableElement> variables)
        {
            Logger.Write("Running ServiceElement for action : " + ServiceAction.ToString());

            switch (ServiceAction)
            {
            case Actions.ChangeStartingMode:
                ChangeStartingMode();
                break;

            case Actions.Start:
                StartService();
                break;

            case Actions.Stop:
                StopService();
                break;

            case Actions.Register:
                RegisterService();
                break;

            case Actions.Unregister:
                UnregisterService();
                break;
            }
            Logger.Write("End of ServiceElement.");
        }
 public IActionResult ChangeService(ServiceAction serviceAction, string machineName, string serviceName)
 => Json(new
 {
     MachineName   = machineName,
     ServiceName   = serviceName,
     ServiceAction = serviceAction.ToString(),
     Succeeded     = _network.ChangeServiceStatus(machineName, serviceName, serviceAction)
 });
 public IActionResult ChangeAllServices(ServiceAction serviceAction, string machineName)
 => Json(ServiceActionToMachine(serviceAction, machineName)
         .Select(x => new
 {
     ServiceName   = x.Key,
     ServiceAction = serviceAction.ToString(),
     Succeeded     = x.Value
 })
         .OrderBy(x => x.ServiceName));
        private string serviceControl(string server, ServiceAction serviceAction, string service)
        {
            Process p = new Process();

            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow         = true;
            p.StartInfo.FileName  = "sc";
            p.StartInfo.Arguments = server + " " + serviceAction.ToString() + " " + service.ToString();
            p.Start();
            string output = p.StandardOutput.ReadToEnd();

            p.WaitForExit();
            return(output);
        }
Exemplo n.º 7
0
    public void ProcessServiceRequest(ServiceAction action, IISAppPool pool, int timeout)
    {
        AppPoolConfig status  = null;
        bool          success = false;

        switch (action)
        {
        case ServiceAction.Create:
            String username = pool.UserName;
            if (!String.IsNullOrWhiteSpace(pool.Domain))
            {
                username = pool.Domain + @"\" + pool.UserName;
            }
            AppPoolUtil.Create(pool.Name, pool.Server, config.Timeout, pool.ManagedRuntimeVersion, (int)pool.IdentityType, username, pool.Password);
            break;

        case ServiceAction.Delete:
            AppPoolUtil.Delete(pool.Name, pool.Server, config.Timeout);
            break;

        case ServiceAction.Query:
            break;

        case ServiceAction.Start:
            success = AppPoolUtil.Start(pool.Name, pool.Server, config.Timeout);
            break;

        case ServiceAction.Stop:
            success = AppPoolUtil.Stop(pool.Name, pool.Server, config.Timeout, 3, 5000);
            break;

        case ServiceAction.Restart:
            success = AppPoolUtil.Recycle(pool.Name, pool.Server, config.Timeout);
            break;

        case ServiceAction.StartMode:
            break;
        }

        status = AppPoolUtil.QueryStatus(pool.Name, false, pool.Server);

        if (status != null)
        {
            OnLogMessage(action.ToString(), "Name : [" + status.AppPoolName + "] Status : [" + status.State + "]");
            results.Add(status);
        }
    }
Exemplo n.º 8
0
    public void ProcessServiceRequest(ServiceAction action, Service service, int timeout)
    {
        string[] loadOrderGroupDependencies = service.LoadOrderGroupDependencies?.ToArray<String>();
        string[] serviceDependencies = service.ServiceDependencies?.ToArray<String>();

        ServiceConfig status = null;
        ServiceReturnCode rc = ServiceReturnCode.NotSupported;
        bool success = _isDryRun;

        switch( action )
        {
            case ServiceAction.Create:
            {
                    if (!_isDryRun)
                    {
                        rc = ServiceUtil.CreateService(service.Name, service.Server, service.DisplayName, service.Description, service.BinPath,
                                                        service.StartMode, service.StartName, service.StartPassword,
                                                        service.Type, service.OnError, service.InteractWithDesktop, service.LoadOrderGroup,
                                                        loadOrderGroupDependencies, serviceDependencies);
                        if (_config.StartOnCreate == true)
                            success = ServiceUtil.Start(service.Name, service.Server, timeout, service.StartMode);
                        status = ServiceUtil.QueryService(service.Name, service.Server);
                    }
                    else
                    {
                        // DryRun : Return mocked up ServiceConfig with values that WOULD have been created.
                        ServiceConfig sc = new ServiceConfig();
                        sc.Name = service.Name;
                        sc.ServerName = service.Server;
                        sc.DisplayName = service.DisplayName;
                        sc.Description = service.Description;
                        sc.PathName = service.BinPath;
                        sc.StartMode = service.StartMode;
                        sc.StartName = service.StartName;
                        sc.ServiceType = service.Type.ToString();
                        sc.ErrorControl = service.OnError.ToString();
                        sc.DesktopInteract = service.InteractWithDesktop;
                        status = sc;
                    }

                    break;
            }
            case ServiceAction.Delete:
            {
                if (!_isDryRun)
                {
                    rc = ServiceUtil.DeleteService(service.Name, service.Server);
                }
                else
                {
                    status = ServiceUtil.QueryService(service.Name, service.Server);
                }
                break;
            }
            case ServiceAction.Query:
            {
                status = ServiceUtil.QueryService( service.Name, service.Server );
                break;
            }
            case ServiceAction.Start:
            {
                if( !_isDryRun )
                    success = ServiceUtil.Start( service.Name, service.Server, timeout, service.StartMode, service.StartParameters?.ToArray<String>() );
                status = ServiceUtil.QueryService( service.Name, service.Server );
                break;
            }
            case ServiceAction.Stop:
            {
                if( !_isDryRun )
                    success = ServiceUtil.Stop( service.Name, service.Server, timeout, service.StartMode );
                status = ServiceUtil.QueryService( service.Name, service.Server );
                break;
            }
            case ServiceAction.Restart:
            {
                if( !_isDryRun )
                {
                    success = ServiceUtil.Stop( service.Name, service.Server, timeout, ServiceStartMode.Unchanged );
                    Thread.Sleep( 5000 );
                    success = ServiceUtil.Start( service.Name, service.Server, timeout, service.StartMode );
                }
                status = ServiceUtil.QueryService( service.Name, service.Server );
            }
            break;
            case ServiceAction.StartMode:
            {
                if( !_isDryRun )
                    rc = ServiceUtil.ChangeStartMode( service.Name, service.Server, service.StartMode );
                status = ServiceUtil.QueryService( service.Name, service.Server );
                break;
            }
        }

        if( status != null )
        {
            OnLogMessage( action.ToString(), "Name : [" + status.ServiceName + "] Status : [" + status.State + "]" );
            _results.Add( status );
        }
    }