コード例 #1
0
        protected override void ProcessRecord()
        {
            // TODO handle stdin
            try
            {
                if (!Path.IsPathRooted(Executable))
                {
                    Executable = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Executable);
                }

                var startInfo = new ProcessStartInfo
                {
                    FileName        = Executable,
                    Arguments       = ArgumentEscaper.Escape(Arguments),
                    UseShellExecute = false,
                    CreateNoWindow  = true
                };

                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError  = true;
                WriteDebug($"Executing process: [{startInfo.FileName}]");
                WriteDebug($"Arguments: [{startInfo.Arguments}]");
                var process = new Process
                {
                    StartInfo = startInfo
                };
                process.Start();
                Forward(process.StandardOutput);
                Forward(process.StandardError);

                string line;
                while (_reading > 0 ||
                       _queue.Count > 0)
                {
                    if (_queue.TryDequeue(out line))
                    {
                        Write(line);
                    }
                }

                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    WriteError(new ErrorRecord(
                                   new AggregateException("Process finished with non-zero exit code"),
                                   $"[{Executable}] finished with exit code {process.ExitCode}",
                                   ErrorCategory.CloseError,
                                   Executable));
                }
            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(
                               ex,
                               ex.Message,
                               ErrorCategory.NotSpecified,
                               nameof(InvokeProcessCommand)));
            }
        }
コード例 #2
0
 public void EscapesArguments(string[] args, string expected)
 => Assert.Equal(expected, ArgumentEscaper.Escape(args));