コード例 #1
0
 private void CalculateIORedirection(
     out bool redirectOutput,
     out bool redirectError,
     out bool redirectInput)
 {
     redirectInput  = true;
     redirectOutput = true;
     redirectError  = true;
     if (this.Command.MyInvocation.PipelinePosition == this.Command.MyInvocation.PipelineLength)
     {
         if (this.context.IsTopLevelPipe(this.commandRuntime.OutputPipe))
         {
             redirectOutput = false;
         }
         else
         {
             CommandProcessorBase downstreamCmdlet = this.commandRuntime.OutputPipe.DownstreamCmdlet;
             if (downstreamCmdlet != null && string.Equals(downstreamCmdlet.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectOutput = false;
             }
         }
     }
     if (!this.CommandRuntime.MergeMyErrorOutputWithSuccess)
     {
         if (this.context.IsTopLevelPipe(this.commandRuntime.ErrorOutputPipe))
         {
             redirectError = false;
         }
         else
         {
             CommandProcessorBase downstreamCmdlet = this.commandRuntime.ErrorOutputPipe.DownstreamCmdlet;
             if (downstreamCmdlet != null && string.Equals(downstreamCmdlet.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectError = false;
             }
         }
     }
     if (!redirectError && redirectOutput && this.isMiniShell)
     {
         redirectError = true;
     }
     if (this.inputWriter.Count == 0 && !this.Command.MyInvocation.ExpectingInput)
     {
         redirectInput = false;
     }
     if (NativeCommandProcessor.IsServerSide)
     {
         redirectInput  = true;
         redirectOutput = true;
         redirectError  = true;
     }
     else if (NativeCommandProcessor.IsConsoleApplication(this.Path))
     {
         ConsoleVisibility.AllocateHiddenConsole();
         if (ConsoleVisibility.AlwaysCaptureApplicationIO)
         {
             redirectOutput = true;
             redirectError  = true;
         }
     }
     if (redirectInput || redirectOutput)
     {
         return;
     }
     this._runStandAlone = true;
 }
コード例 #2
0
        internal override void Complete()
        {
            bool redirectOutput;
            bool redirectError;
            bool redirectInput;

            this.CalculateIORedirection(out redirectOutput, out redirectError, out redirectInput);
            bool             soloCommand      = this.Command.MyInvocation.PipelineLength == 1;
            ProcessStartInfo processStartInfo = this.GetProcessStartInfo(redirectOutput, redirectError, redirectInput, soloCommand);

            if (this.Command.Context.CurrentPipelineStopping)
            {
                throw new PipelineStoppedException();
            }
            Exception innerException = (Exception)null;

            try
            {
                if (!redirectOutput)
                {
                    this.Command.Context.EngineHostInterface.NotifyBeginApplication();
                }
                lock (this.sync)
                {
                    if (this.stopped)
                    {
                        throw new PipelineStoppedException();
                    }
                    try
                    {
                        this.nativeProcess           = new Process();
                        this.nativeProcess.StartInfo = processStartInfo;
                        this.nativeProcess.Start();
                    }
                    catch (Win32Exception ex1)
                    {
                        string executable = NativeCommandProcessor.FindExecutable(processStartInfo.FileName);
                        bool   flag       = true;
                        if (!string.IsNullOrEmpty(executable))
                        {
                            if (NativeCommandProcessor.IsConsoleApplication(executable))
                            {
                                ConsoleVisibility.AllocateHiddenConsole();
                            }
                            string arguments = processStartInfo.Arguments;
                            string fileName  = processStartInfo.FileName;
                            processStartInfo.Arguments = "\"" + processStartInfo.FileName + "\" " + processStartInfo.Arguments;
                            processStartInfo.FileName  = executable;
                            try
                            {
                                this.nativeProcess.Start();
                                flag = false;
                            }
                            catch (Win32Exception ex2)
                            {
                                processStartInfo.Arguments = arguments;
                                processStartInfo.FileName  = fileName;
                            }
                        }
                        if (flag)
                        {
                            if (soloCommand && !processStartInfo.UseShellExecute)
                            {
                                processStartInfo.UseShellExecute        = true;
                                processStartInfo.RedirectStandardInput  = false;
                                processStartInfo.RedirectStandardOutput = false;
                                processStartInfo.RedirectStandardError  = false;
                                this.nativeProcess.Start();
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
                bool flag1;
                if (this.Command.MyInvocation.PipelinePosition < this.Command.MyInvocation.PipelineLength)
                {
                    flag1 = false;
                }
                else
                {
                    flag1 = true;
                    if (!processStartInfo.UseShellExecute)
                    {
                        flag1 = NativeCommandProcessor.IsWindowsApplication(this.nativeProcess.StartInfo.FileName);
                    }
                }
                try
                {
                    if (processStartInfo.RedirectStandardInput)
                    {
                        NativeCommandIOFormat inputFormat = NativeCommandIOFormat.Text;
                        if (this.isMiniShell)
                        {
                            inputFormat = ((MinishellParameterBinderController)this.NativeParameterBinderController).InputFormat;
                        }
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.inputWriter.Start(this.nativeProcess, inputFormat);
                            }
                        }
                    }
                    if (!flag1)
                    {
                        if (!processStartInfo.RedirectStandardOutput)
                        {
                            if (!processStartInfo.RedirectStandardError)
                            {
                                goto label_54;
                            }
                        }
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.outputReader = new ProcessOutputReader(this.nativeProcess, this.Path, redirectOutput, redirectError);
                                this.outputReader.Start();
                            }
                        }
                        if (this.outputReader != null)
                        {
                            this.ProcessOutputHelper();
                        }
                    }
                }
                catch (Exception ex)
                {
                    NativeCommandProcessor.KillProcess(this.nativeProcess);
                    throw;
                }
                finally
                {
                    if (!flag1)
                    {
                        this.nativeProcess.WaitForExit();
                        this.inputWriter.Done();
                        if (this.outputReader != null)
                        {
                            this.outputReader.Done();
                        }
                        this.Command.Context.SetVariable("global:LASTEXITCODE", (object)this.nativeProcess.ExitCode);
                        if (this.nativeProcess.ExitCode != 0)
                        {
                            this.commandRuntime.PipelineProcessor.ExecutionFailed = true;
                        }
                    }
                }
            }
            catch (Win32Exception ex)
            {
                innerException = (Exception)ex;
            }
            catch (PipelineStoppedException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                CommandProcessorBase.CheckForSevereException(ex);
                innerException = ex;
            }
            finally
            {
                if (!redirectOutput)
                {
                    this.Command.Context.EngineHostInterface.NotifyEndApplication();
                }
                this.CleanUp();
            }
label_54:
            if (innerException != null)
            {
                string message = ResourceManagerCache.FormatResourceString("Parser", "ProgramFailedToExecute", (object)this.NativeCommandName, (object)innerException.Message, (object)this.Command.MyInvocation.PositionMessage);
                if (message == null)
                {
                    message = StringUtil.Format("Program '{0}' failed to execute: {1}{2}", (object)this.NativeCommandName, (object)innerException.Message, (object)this.Command.MyInvocation.PositionMessage);
                }
                ApplicationFailedException applicationFailedException = new ApplicationFailedException(message, innerException);
                NativeCommandProcessor.tracer.TraceException((Exception)applicationFailedException);
                throw applicationFailedException;
            }
        }