/// <summary> /// Run a given command with privileges using current user. /// </summary> /// <param name="command">Command to launch</param> /// <param name="args">Optional arguments for the command</param> /// <param name="logFilePath">Redirect standard output/error of the command to a file.</param> /// <param name="initVerb">Initial tool verb</param> /// <returns>Process exit code</returns> public int Elevate(string command, IEnumerable<string> arguments, string logFilePath, string initVerb) { /* * The tool will call itself with different arguments and then launch the command asked by the user. * * runas "verb" force the usage of UseShellExecute which disable input/output redirection. * The child process will write to a file instead. * TODO: is there a better way ?? */ // Check if the tool was launched directly by user using "elevate" command verb. In that case, it need to display the process output at the end. var ftc = new FileToConsoleHandler(); if (string.IsNullOrEmpty(initVerb)) { logFilePath = ftc.CreateNewFilePath(logFilePath); } // Init bootstrap var bootstrapCommand = Utils.GetExecutingProgramPath(); var bootstrapOptions = new BootstrapOptions() { LogFilePath = logFilePath, Command = command, Arguments = arguments, InitVerb = initVerb.NullIfEmpty() ?? "elevate" }; var info = new ProcessStartInfo(bootstrapCommand); info.CreateNoWindow = true; info.UseShellExecute = true; info.Verb = "runas"; info.Arguments = parser.FormatCommandLine(bootstrapOptions); // Exec var proc = new ProcessLauncher(); int procExitCode = proc.Exec(info); // Write output if needed ftc.SafeWriteToConsole(); // Return exit code return procExitCode; }
/// <summary> /// Run a given command using a different user (with privileges if needed). /// </summary> /// <param name="command">Command to launch</param> /// <param name="args">Optional arguments for the command</param> /// <param name="domain">Optional user domain to run the command as</param> /// <param name="userName">Username to run the command as</param> /// <param name="passwordSecured">Password</param> /// <param name="elevate">Whether to run the command with privileges rights or not. Default is limited</param> /// <returns>Process exit code</returns> public int RunAs(string command, IEnumerable<string> args, string domain, string userName, SecureString passwordSecured, bool elevate) { /* * The tool will call itself with different arguments and then launch the command asked by the user. */ var bootstrapCommand = Utils.GetExecutingProgramPath(); var info = new ProcessStartInfo(bootstrapCommand); info.CreateNoWindow = true; info.UseShellExecute = false; info.LoadUserProfile = true; info.Domain = domain; info.UserName = userName; info.Password = passwordSecured; // Init bootstrap var ftc = new FileToConsoleHandler(); if (!elevate) { var bootstrapOptions = new BootstrapOptions() { Command = command, Arguments = args, InitVerb = "runas" }; info.Arguments = this.parser.FormatCommandLine(bootstrapOptions); } else { var elevateOptions = new ElevateOptions() { LogFilePath = ftc.CreateNewFilePath(), Command = command, Arguments = args, InitVerb = "runas" }; info.Arguments = this.parser.FormatCommandLine(elevateOptions); } // Exec int procExitCode; using (var procEventHandler = new ProcessEventHandler()) { var proc = new ProcessLauncher(); procExitCode = proc.Exec(info, procEventHandler); } // Write output if needed ftc.SafeWriteToConsole(); // Return exit code return procExitCode; }