Пример #1
0
        private void StartService()
        {
            if (!(ServiceInstalled && !ServiceCanStop))
            {
                return;
            }
            string errorRepresent;

            if (!ServiceControll.StartService(Scheduler.currentServiceName, out errorRepresent))
            {
                MessageBox.Show($"Operation failed. {errorRepresent}");
            }
            NotifyAllPropertyChanged();
        }
Пример #2
0
        private void ChangeLogonAccount()
        {
            uint errorCode;

            if (!ServiceControll.SetServiceLogon(Scheduler.currentServiceName, out errorCode))
            {
                MessageBox.Show($"Operation failed. Error code {errorCode}");
            }
            var logonControl = new LogonControlViewModel();

            if (!logonControl.AccountMatch &&
                MessageBox.Show("Run this app under service account now?", "Sisyphus",
                                MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                LogonControlViewModel.ChangeLogonAccount();
            }
            NotifyAllPropertyChanged();
        }
Пример #3
0
        public static void ChangeLogonAccount(NetworkCredential credential = null)
        {
            ProcessStartInfo startInfo;

            if (ServiceControll.GetServiceLogonName(Scheduler.currentServiceName).ToLower() == "localsystem")
            {
                startInfo = new ProcessStartInfo()
                {
                    FileName        = "psexec.exe",
                    Verb            = "runas",
                    UseShellExecute = true,
                    // ReSharper disable once AssignNullToNotNullAttribute
                    WorkingDirectory = Path.GetDirectoryName(Application.ResourceAssembly.Location),
                    Arguments        = $"-i -d -s {Application.ResourceAssembly.Location}",
                };
                try
                {
                    Process.Start(startInfo);
                    Application.Current.Shutdown();
                }
                catch (Exception e)
                {
                    MessageBox.Show($"Operation failed. Error represent: {e.Message}");
                }
                return;
            }
            if (credential == null)
            {
                CommonFunctions.GetCredentialsVistaAndUp(out credential, $"Please enter password for account {ServiceControll.GetServiceLogonName(Scheduler.currentServiceName)}", "Leave account name field empty.");
            }
            if (credential == null)
            {
                return;
            }
            if (credential.UserName == "")
            {
                credential.UserName = ServiceControll.GetServiceLogonName(Scheduler.currentServiceName).GetUserName(); credential.Domain = ServiceControll.GetServiceLogonName(Scheduler.currentServiceName).GetDomain();
            }

            startInfo = new ProcessStartInfo()
            {
                Domain          = credential.Domain,
                UserName        = credential.UserName,
                Password        = new SecureString(),
                FileName        = Application.ResourceAssembly.Location,
                UseShellExecute = false,
                // ReSharper disable once AssignNullToNotNullAttribute
                WorkingDirectory = Path.GetDirectoryName(Application.ResourceAssembly.Location),
            };
            credential.Password.ToCharArray().ToList().ForEach(p => startInfo.Password.AppendChar(p));

            try
            {
                Process.Start(startInfo);
                Application.Current.Shutdown();
            }
            catch (Exception e)
            {
                MessageBox.Show($"Operation failed. Error represent: {e.Message}");
            }
        }
Пример #4
0
 private void UninstallService()
 {
     ServiceControll.UninstallService(Scheduler.currentServiceName); NotifyAllPropertyChanged();
 }
Пример #5
0
        public static bool RunElevated(string serviceName, out string errorRepresent, out uint errorCode, StartupMode startupMode = StartupMode.Auto,
                                       [CallerMemberName] string methodName = "")
        {
            errorRepresent = "";
            errorCode      = 0;
            var tempFileName = Path.GetTempFileName();
            var startInfo    = new ProcessStartInfo
            {
                FileName         = Assembly.GetCallingAssembly().Location,
                WorkingDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? "",
                Verb             = "runas",
                CreateNoWindow   = true,
                UseShellExecute  = true,
                Arguments        = $"-m {methodName} -s {serviceName} -o {tempFileName} -d {ServiceControll.StartupModeToString(startupMode)}"
            };
            var process = new Process {
                StartInfo = startInfo
            };

            process.Start();
            process.WaitForExit();
            try
            {
                var resultFileContent = File.ReadAllLines(tempFileName);
                errorCode      = Convert.ToUInt16(resultFileContent[1]);
                errorRepresent = Convert.ToString(resultFileContent[2]);
                return(Convert.ToBoolean(resultFileContent[0]));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #6
0
        // Consume them
        static void Main(string[] args)
        {
            var options = new Options();

            if (!Parser.Default.ParseArguments(args, options))
            {
                return;
            }
            if (options.debug)
            {
                Console.ReadLine();
            }
            ServiceControll.SeparatedProcess = true;
            if (options.MethodName == null)
            {
                return;
            }
            FileStream fs = null;

            if (options.Output != "")
            {
                try
                {
                    fs = new FileStream(options.Output, FileMode.OpenOrCreate, FileAccess.Write);
                    var sw = new StreamWriter(fs);
                    Console.SetOut(sw);
                }
                catch (Exception e) { Console.WriteLine(e.Message); }
            }
            var  errorRepresent = "";
            var  result         = false;
            uint errorCode      = 0;

            try
            {
                switch (options.MethodName)
                {
                case nameof(ServiceControll.StartService):
                {
                    result = ServiceControll.StartService(options.ServiceName, out errorRepresent);
                    break;
                }

                case nameof(ServiceControll.StopService):
                {
                    result = ServiceControll.StopService(options.ServiceName, out errorRepresent);
                    break;
                }

                case nameof(ServiceControll.InstallService):
                {
                    ServiceControll.InstallService(options.ServiceName);
                    break;
                }

                case nameof(ServiceControll.UninstallService):
                {
                    ServiceControll.UninstallService(options.ServiceName);
                    break;
                }

                case nameof(ServiceControll.SetServiceLogon):
                {
                    result = ServiceControll.SetServiceLogon(options.ServiceName, out errorCode);
                    break;
                }

                case nameof(ServiceControll.SetServiceStartupMode):
                {
                    result = ServiceControll.SetServiceStartupMode(options.ServiceName,
                                                                   ServiceControll.StringToStartupMode(options.StartupMode), out errorRepresent);
                    break;
                }

                default:
                {
                    result         = false;
                    errorRepresent = $"Method doesn't exist. {options.MethodName}";
                    break;
                }
                }
            }
            catch (Exception e)
            {
                result         = false;
                errorRepresent = e.Message;
            }
            Console.WriteLine(result);
            Console.WriteLine(errorCode);
            Console.WriteLine(errorRepresent);
            Console.WriteLine(options.MethodName);
            Console.Out.Close();
            fs?.Close();
        }