Exemplo n.º 1
0
        public void should_support_without_no_additional_stream()
        {
            var consoleStream = new Mock<TextWriter>();
            var data = "hello";

            var peh = new ProcessEventHandler();
            peh.ForwardToStream(consoleStream.Object, data);

            consoleStream.Verify(x => x.WriteLine(data), Times.Once);
        }
Exemplo n.º 2
0
        public void should_not_write_to_any_stream_if_null()
        {
            var stream = new Mock<TextWriter>();
            var consoleStream = new Mock<TextWriter>();

            var peh = new ProcessEventHandler(stream.Object);
            peh.ForwardToStream(consoleStream.Object, null);

            stream.Verify(x => x.WriteLine(It.IsAny<string>()), Times.Never);
            consoleStream.Verify(x => x.WriteLine(It.IsAny<string>()), Times.Never);
        }
Exemplo n.º 3
0
        public void should_write_to_all_stream_if_not_null()
        {
            var stream = new Mock<TextWriter>();
            var consoleStream = new Mock<TextWriter>();
            var data = "hello";

            var peh = new ProcessEventHandler(stream.Object);
            peh.ForwardToStream(consoleStream.Object, data);

            stream.Verify(x => x.WriteLine(data), Times.Once);
            consoleStream.Verify(x => x.WriteLine(data), Times.Once);
        }
Exemplo n.º 4
0
 private void RaiseError(ProcessEventHandler eventHandler, string errorFormat, params object[] args)
 {
     var message = string.Format(errorFormat, args);
     if (eventHandler != null)
     {
         eventHandler.ForwardToStream(Console.Error, message);
     }
     else
     {
         Console.Error.WriteLine(message);
     }
 }
Exemplo n.º 5
0
        public int Exec(ProcessStartInfo info, ProcessEventHandler eventHandler = null)
        {
            var hasEventHandler = eventHandler != null;

            if (hasEventHandler)
            {
                info.RedirectStandardError = true;
                info.RedirectStandardOutput = true;
            }

            using (var proc = new Process())
            {
                proc.StartInfo = info;
                if (hasEventHandler)
                {
                    proc.OutputDataReceived += eventHandler.OutputDataReceived;
                    proc.ErrorDataReceived += eventHandler.ErrorDataReceived;
                }

                try
                {
                    proc.Start();
                }
                catch (Win32Exception ex)
                {
                    RaiseError(eventHandler, @"Cannot start ""{0}"": {1}", info.FileName, ex.Message);
                    return 1;
                }

                if (hasEventHandler)
                {
                    proc.BeginErrorReadLine();
                    proc.BeginOutputReadLine();
                }

                try
                {
                    proc.WaitForExit();
                }
                catch (Win32Exception ex)
                {
                    RaiseError(eventHandler, @"An error occurred while waiting for the child process ""{0}"": {1}", info.FileName, ex.Message);
                    return 1;
                }

                return proc.ExitCode;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Run a given command using current user and privileges.
        /// Output will be displayed in the console and written to the log file path if given.
        /// </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 Bootstrap(string command, IEnumerable<string> arguments, string logFilePath, string initVerb)
        {
            /*
             * The tool will launch the command asked by the user.
             */

            var info = new ProcessStartInfo(command);
            info.UseShellExecute = false;
            info.Arguments = Utils.StringifyArguments(arguments);

            using (var procEventHandler = new ProcessEventHandler(logFilePath))
            {
                var proc = new ProcessLauncher();
                return proc.Exec(info, procEventHandler);
            }
        }
Exemplo n.º 7
0
        /// <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;
        }