static void RunProcessWithCredentials(ProcessStartInfo processStartInfo, string userName, SecureString password)
        {
            if (string.IsNullOrEmpty(userName) || password == null)
            {
                return;
            }

            var parts = userName.Split(new[] { '\\' }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 2)
            {
                var domainPart   = parts[0];
                var userNamePart = parts[1];
                processStartInfo.Domain   = domainPart;
                processStartInfo.UserName = userNamePart;

                WindowStationAndDesktopAccess.GrantAccessToWindowStationAndDesktop(userNamePart, domainPart);
            }
            else
            {
                processStartInfo.UserName = userName;

                WindowStationAndDesktopAccess.GrantAccessToWindowStationAndDesktop(userName);
            }

            processStartInfo.Password = password;

            // Environment variables (such as {env:TentacleHome}) are usually inherited from the parent process.
            // When running as a different user they are not inherited, so manually add them to the process.
            AddTentacleEnvironmentVariablesToProcess(processStartInfo);
        }
Beispiel #2
0
        public static int ExecuteCommand(string executable, string arguments, string workingDirectory, string userName, SecureString password, Action <string> output, Action <string> error)
        {
            try
            {
                using (var process = new Process())
                {
                    process.StartInfo.FileName               = executable;
                    process.StartInfo.Arguments              = arguments;
                    process.StartInfo.WorkingDirectory       = workingDirectory;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.StandardOutputEncoding = oemEncoding;
                    process.StartInfo.StandardErrorEncoding  = oemEncoding;

#if CAN_RUN_PROCESS_AS
                    if (!string.IsNullOrEmpty(userName) && password != null)
                    {
                        process.StartInfo.UserName = userName;
                        process.StartInfo.Password = password;

                        WindowStationAndDesktopAccess.GrantAccessToWindowStationAndDesktop(userName);

                        // Environment variables (such as {env:TentacleHome}) are usually inherited from the parent process.
                        // When running as a different user they are not inherited, so manually add them to the process.
                        AddTentacleEnvironmentVariablesToProcess(process.StartInfo);
                    }
#endif

                    using (var outputWaitHandle = new AutoResetEvent(false))
                        using (var errorWaitHandle = new AutoResetEvent(false))
                        {
                            process.OutputDataReceived += (sender, e) =>
                            {
                                try
                                {
                                    if (e.Data == null)
                                    {
                                        outputWaitHandle.Set();
                                    }
                                    else
                                    {
                                        output(e.Data);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    try
                                    {
                                        error($"Error occured handling message: {ex.PrettyPrint()}");
                                    }
                                    catch
                                    {
                                        // Ignore
                                    }
                                }
                            };

                            process.ErrorDataReceived += (sender, e) =>
                            {
                                try
                                {
                                    if (e.Data == null)
                                    {
                                        errorWaitHandle.Set();
                                    }
                                    else
                                    {
                                        error(e.Data);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    try
                                    {
                                        error($"Error occured handling message: {ex.PrettyPrint()}");
                                    }
                                    catch
                                    {
                                        // Ignore
                                    }
                                }
                            };

                            process.Start();

                            process.BeginOutputReadLine();
                            process.BeginErrorReadLine();

                            process.WaitForExit();

                            outputWaitHandle.WaitOne();
                            errorWaitHandle.WaitOne();

                            return(process.ExitCode);
                        }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Error when attempting to execute {executable}: {ex.Message}", ex);
            }
        }