コード例 #1
0
        internal override void Complete()
        {
            bool flag2;
            bool flag3;
            bool flag4;

            if (base.Context._debuggingMode > 0)
            {
                base.Context.Debugger.CheckCommand(base.Command.MyInvocation);
            }
            this.CalculateIORedirection(out flag2, out flag3, out flag4);
            bool             soloCommand = base.Command.MyInvocation.PipelineLength == 1;
            ProcessStartInfo info        = this.GetProcessStartInfo(flag2, flag3, flag4, soloCommand);

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

            try
            {
                bool flag;
                if (!flag2)
                {
                    base.Command.Context.EngineHostInterface.NotifyBeginApplication();
                }
                lock (this.sync)
                {
                    if (this.stopped)
                    {
                        throw new PipelineStoppedException();
                    }
                    try
                    {
                        this.nativeProcess           = new Process();
                        this.nativeProcess.StartInfo = info;
                        this.nativeProcess.Start();
                    }
                    catch (Win32Exception)
                    {
                        string str   = FindExecutable(info.FileName);
                        bool   flag6 = true;
                        if (!string.IsNullOrEmpty(str))
                        {
                            if (IsConsoleApplication(str))
                            {
                                ConsoleVisibility.AllocateHiddenConsole();
                            }
                            string arguments = info.Arguments;
                            string fileName  = info.FileName;
                            info.Arguments = "\"" + info.FileName + "\" " + info.Arguments;
                            info.FileName  = str;
                            try
                            {
                                this.nativeProcess.Start();
                                flag6 = false;
                            }
                            catch (Win32Exception)
                            {
                                info.Arguments = arguments;
                                info.FileName  = fileName;
                            }
                        }
                        if (flag6)
                        {
                            if (!soloCommand || info.UseShellExecute)
                            {
                                throw;
                            }
                            info.UseShellExecute        = true;
                            info.RedirectStandardInput  = false;
                            info.RedirectStandardOutput = false;
                            info.RedirectStandardError  = false;
                            this.nativeProcess.Start();
                        }
                    }
                }
                if (base.Command.MyInvocation.PipelinePosition < base.Command.MyInvocation.PipelineLength)
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                    if (!info.UseShellExecute)
                    {
                        flag = IsWindowsApplication(this.nativeProcess.StartInfo.FileName);
                    }
                }
                try
                {
                    if (info.RedirectStandardInput)
                    {
                        NativeCommandIOFormat text = NativeCommandIOFormat.Text;
                        if (this.isMiniShell)
                        {
                            text = ((MinishellParameterBinderController)this.NativeParameterBinderController).InputFormat;
                        }
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.inputWriter.Start(this.nativeProcess, text);
                            }
                        }
                    }
                    if (!flag && (info.RedirectStandardOutput || info.RedirectStandardError))
                    {
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.outputReader = new ProcessOutputReader(this.nativeProcess, this.Path, flag2, flag3);
                                this.outputReader.Start();
                            }
                        }
                        if (this.outputReader != null)
                        {
                            this.ProcessOutputHelper();
                        }
                    }
                }
                catch (Exception)
                {
                    this.StopProcessing();
                    throw;
                }
                finally
                {
                    if (!flag)
                    {
                        this.nativeProcess.WaitForExit();
                        this.inputWriter.Done();
                        if (this.outputReader != null)
                        {
                            this.outputReader.Done();
                        }
                        base.Command.Context.SetVariable(SpecialVariables.LastExitCodeVarPath, this.nativeProcess.ExitCode);
                        if (this.nativeProcess.ExitCode != 0)
                        {
                            base.commandRuntime.PipelineProcessor.ExecutionFailed = true;
                        }
                    }
                }
            }
            catch (Win32Exception exception2)
            {
                innerException = exception2;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (Exception exception3)
            {
                CommandProcessorBase.CheckForSevereException(exception3);
                innerException = exception3;
            }
            finally
            {
                if (!flag2)
                {
                    base.Command.Context.EngineHostInterface.NotifyEndApplication();
                }
                this.CleanUp();
            }
            if (innerException != null)
            {
                string message = StringUtil.Format(ParserStrings.ProgramFailedToExecute, new object[] { this.NativeCommandName, innerException.Message, base.Command.MyInvocation.PositionMessage });
                if (message == null)
                {
                    message = StringUtil.Format("Program '{0}' failed to execute: {1}{2}", new object[] { this.NativeCommandName, innerException.Message, base.Command.MyInvocation.PositionMessage });
                }
                ApplicationFailedException exception4 = new ApplicationFailedException(message, innerException);
                throw exception4;
            }
        }
コード例 #2
0
 private void CalculateIORedirection(out bool redirectOutput, out bool redirectError, out bool redirectInput)
 {
     redirectInput  = true;
     redirectOutput = true;
     redirectError  = true;
     if (base.Command.MyInvocation.PipelinePosition == base.Command.MyInvocation.PipelineLength)
     {
         if (base._context.IsTopLevelPipe(base.commandRuntime.OutputPipe))
         {
             redirectOutput = false;
         }
         else
         {
             CommandProcessorBase downstreamCmdlet = base.commandRuntime.OutputPipe.DownstreamCmdlet;
             if ((downstreamCmdlet != null) && string.Equals(downstreamCmdlet.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectOutput = false;
             }
         }
     }
     if (base.CommandRuntime.ErrorMergeTo != MshCommandRuntime.MergeDataStream.Output)
     {
         if (base._context.IsTopLevelPipe(base.commandRuntime.ErrorOutputPipe))
         {
             redirectError = false;
         }
         else
         {
             CommandProcessorBase base3 = base.commandRuntime.ErrorOutputPipe.DownstreamCmdlet;
             if ((base3 != null) && string.Equals(base3.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectError = false;
             }
         }
     }
     if ((!redirectError && redirectOutput) && this.isMiniShell)
     {
         redirectError = true;
     }
     if ((this.inputWriter.Count == 0) && !base.Command.MyInvocation.ExpectingInput)
     {
         redirectInput = false;
     }
     if (IsServerSide)
     {
         redirectInput  = true;
         redirectOutput = true;
         redirectError  = true;
     }
     else if (IsConsoleApplication(this.Path))
     {
         ConsoleVisibility.AllocateHiddenConsole();
         if (ConsoleVisibility.AlwaysCaptureApplicationIO)
         {
             redirectOutput = true;
             redirectError  = true;
         }
     }
     if (!redirectInput && !redirectOutput)
     {
         this._runStandAlone = true;
     }
 }
コード例 #3
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;
            }
        }