Exemplo n.º 1
0
 internal void Start(Process process, NativeCommandIOFormat inputFormat)
 {
     if (!(this.command.Context.GetVariable("OutputEncoding") is Encoding encoding))
     {
         encoding = Encoding.ASCII;
     }
     this.streamWriter = new StreamWriter(process.StandardInput.BaseStream, encoding);
     this.inputFormat  = inputFormat;
     if (inputFormat == NativeCommandIOFormat.Text)
     {
         this.ConvertToString();
     }
     this.inputThread = new Thread(new ThreadStart(this.WriterThreadProc));
     this.inputThread.Start();
 }
Exemplo n.º 2
0
 internal void Start(Process process, NativeCommandIOFormat inputFormat)
 {
     Encoding variableValue = this.command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as Encoding;
     if (variableValue == null)
     {
         variableValue = Encoding.ASCII;
     }
     this.streamWriter = new StreamWriter(process.StandardInput.BaseStream, variableValue);
     this.inputFormat = inputFormat;
     if (inputFormat == NativeCommandIOFormat.Text)
     {
         this.ConvertToString();
     }
     this.inputThread = new Thread(new ThreadStart(this.WriterThreadProc));
     this.inputThread.Start();
 }
Exemplo n.º 3
0
        internal void Start(Process process, NativeCommandIOFormat inputFormat)
        {
            Encoding variableValue = this.command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as Encoding;

            if (variableValue == null)
            {
                variableValue = Encoding.ASCII;
            }
            this.streamWriter = new StreamWriter(process.StandardInput.BaseStream, variableValue);
            this.inputFormat  = inputFormat;
            if (inputFormat == NativeCommandIOFormat.Text)
            {
                this.ConvertToString();
            }
            this.inputThread = new Thread(new ThreadStart(this.WriterThreadProc));
            this.inputThread.Start();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Start writing input to process
        /// </summary>
        /// <param name="process">
        /// process to which input is written
        /// </param>
        /// <param name="inputFormat">
        /// </param>
        internal void Start(Process process, NativeCommandIOFormat inputFormat)
        {
            Dbg.Assert(process != null, "caller should validate the parameter");

            //Get the encoding for writing to native command. Note we get the Encoding 
            //from the current scope so a script or function can use a different encoding
            //than global value.
            Encoding pipeEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as System.Text.Encoding ??
                                    Encoding.ASCII;

            _streamWriter = new StreamWriter(process.StandardInput.BaseStream,
                                            pipeEncoding);
            _inputFormat = inputFormat;

            if (inputFormat == NativeCommandIOFormat.Text)
            {
                ConvertToString();
            }
            _inputThread = new Thread(new ThreadStart(this.WriterThreadProc));
            _inputThread.Start();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Binds the specified parameters to the native command.
        /// </summary>
        /// <param name="parameters">
        /// The parameters to bind.
        /// </param>
        /// <param name="outputRedirected">
        /// true if minishell output is redirected.
        /// </param>
        /// <param name="hostName">
        /// name of the calling host.
        /// </param>
        /// <remarks>
        /// For any parameters that do not have a name, they are added to the command
        /// line arguments for the command
        /// </remarks>
        internal Collection<CommandParameterInternal> BindParameters(Collection<CommandParameterInternal> parameters, bool outputRedirected, string hostName)
        {
            MinishellParameters seen = 0;
            string inputFormat = null;
            string outputFormat = null;
            for (int i = 0; i < parameters.Count; i++)
            {
                var parameter = parameters[i];

                if (parameter.ParameterNameSpecified)
                {
                    var parameterName = parameter.ParameterName;

                    if (CommandParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Command, CommandParameter);

                        // Value must be specified for -Command parameter.
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, CommandParameter,
                                                               typeof(ScriptBlock), null,
                                                               NativeCP.NoValueForCommandParameter,
                                                               "NoValueForCommandParameter");
                        }

                        i += 1;

                        // Value of -Command parameter must be scriptblock
                        var scriptBlockArgument = parameters[i];
                        var argumentValue = PSObject.Base(scriptBlockArgument.ArgumentValue);
                        if (!scriptBlockArgument.ArgumentSpecified || argumentValue is not ScriptBlock)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, CommandParameter,
                                                               typeof(ScriptBlock), argumentValue.GetType(),
                                                               NativeCP.IncorrectValueForCommandParameter,
                                                               "IncorrectValueForCommandParameter");
                        }

                        // Replace the parameters with -EncodedCommand <base64 encoded scriptblock>
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(EncodedCommandParameter, "-" + EncodedCommandParameter, parameter.ParameterAst);
                        string encodedScript = StringToBase64Converter.StringToBase64String(argumentValue.ToString());
                        parameters[i] = CommandParameterInternal.CreateArgument(encodedScript, scriptBlockArgument.ArgumentAst);
                    }
                    else if (InputFormatParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.InputFormat, InputFormatParameter);

                        // Value for -Inputformat must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, InputFormatParameter,
                                                               typeof(string), null,
                                                               NativeCP.NoValueForInputFormatParameter,
                                                               "NoValueForInputFormatParameter");
                        }

                        // Update the argument (partial arguments are allowed)
                        i += 1;
                        var inputFormatArg = parameters[i];
                        inputFormat = ProcessFormatParameterValue(InputFormatParameter, inputFormatArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(InputFormatParameter, "-" + InputFormatParameter, parameter.ParameterAst);
                        parameters[i] = CommandParameterInternal.CreateArgument(inputFormat, inputFormatArg.ArgumentAst);
                    }
                    else if (OutputFormatParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.OutputFormat, OutputFormatParameter);

                        // Value for -Inputformat must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, OutputFormatParameter,
                                                               typeof(string), null,
                                                               NativeCP.NoValueForOutputFormatParameter,
                                                               "NoValueForInputFormatParameter");
                        }

                        // Update the argument (partial arguments are allowed)
                        i += 1;
                        var outputFormatArg = parameters[i];
                        outputFormat = ProcessFormatParameterValue(OutputFormatParameter, outputFormatArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(OutputFormatParameter, "-" + OutputFormatParameter, parameter.ParameterAst);
                        parameters[i] = CommandParameterInternal.CreateArgument(outputFormat, outputFormatArg.ArgumentAst);
                    }
                    else if (ArgsParameter.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase))
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Arguments, ArgsParameter);

                        // Value for -Args parameter must be specified
                        if (i + 1 >= parameters.Count)
                        {
                            throw NewParameterBindingException(null, ErrorCategory.InvalidArgument, ArgsParameter,
                                                               typeof(string), null, NativeCP.NoValuesSpecifiedForArgs,
                                                               "NoValuesSpecifiedForArgs");
                        }

                        // Get the encoded value for -args parameter
                        i += 1;
                        var argsArg = parameters[i];
                        var encodedArgs = ConvertArgsValueToEncodedString(argsArg.ArgumentValue);
                        parameters[i - 1] = CommandParameterInternal.CreateParameter(EncodedArgsParameter, "-" + EncodedArgsParameter, parameter.ParameterAst);
                        // NOTE: do not pass the ArgumentAst; it will fail validation in BindParameters if there
                        // are multiple arguments (array) but encodedArgs is an encoded string.
                        parameters[i] = CommandParameterInternal.CreateArgument(encodedArgs);
                    }
                }
                else
                {
                    // -Command is positional parameter. Bind first scriptblock to it, others are errors.
                    var scriptBlockArgument = parameters[i];
                    var argumentValue = PSObject.Base(scriptBlockArgument.ArgumentValue);
                    if (argumentValue is ScriptBlock)
                    {
                        HandleSeenParameter(ref seen, MinishellParameters.Command, CommandParameter);

                        // Replace the argument with -EncodedCommand <base64 encoded scriptblock>
                        string encodedScript = StringToBase64Converter.StringToBase64String(argumentValue.ToString());
                        parameters[i] = CommandParameterInternal.CreateParameterWithArgument(
                            parameter.ArgumentAst, EncodedCommandParameter, "-" + EncodedCommandParameter,
                            parameter.ArgumentAst, encodedScript,
                            spaceAfterParameter: true);
                    }
                }
            }

            // Add InputFormat and OutputFormat parameter if not specified
            if (inputFormat == null)
            {
                // For minishell default input format is xml
                parameters.Add(CommandParameterInternal.CreateParameter(InputFormatParameter, "-" + InputFormatParameter));
                parameters.Add(CommandParameterInternal.CreateArgument(XmlFormatValue));
                inputFormat = XmlFormatValue;
            }

            if (outputFormat == null)
            {
                // If output is redirected, output format should be xml
                outputFormat = outputRedirected ? XmlFormatValue : TextFormatValue;
                parameters.Add(CommandParameterInternal.CreateParameter(OutputFormatParameter, "-" + OutputFormatParameter));
                parameters.Add(CommandParameterInternal.CreateArgument(outputFormat));
            }

            // Set the output and input format class variable
            InputFormat = XmlFormatValue.StartsWith(inputFormat, StringComparison.OrdinalIgnoreCase)
                ? NativeCommandIOFormat.Xml
                : NativeCommandIOFormat.Text;
            OutputFormat = XmlFormatValue.StartsWith(outputFormat, StringComparison.OrdinalIgnoreCase)
                ? NativeCommandIOFormat.Xml
                : NativeCommandIOFormat.Text;

            // Note if a minishell is invoked from a non-console host, we need to
            // pass -nonInteractive flag. Our console host's name is "ConsoleHost".
            // Correct check would be see if current host has access to console and
            // pass noninteractive flag if doesn't.
            if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
            {
                NonInteractive = true;
                parameters.Insert(0, CommandParameterInternal.CreateParameter(NonInteractiveParameter, "-" + NonInteractiveParameter));
            }

            ((NativeCommandParameterBinder)DefaultParameterBinder).BindParameters(parameters);

            Diagnostics.Assert(s_emptyReturnCollection.Count == 0, "This list shouldn't be used for anything as it's shared.");

            return s_emptyReturnCollection;
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
0
 private ArrayList ProcessMinishellParameters(
     ArrayList args,
     bool outputRedirected,
     string hostName)
 {
     using (MinishellParameterBinderController.tracer.TraceMethod())
     {
         ArrayList arrayList = new ArrayList();
         string    lhs1      = (string)null;
         string    lhs2      = (string)null;
         MinishellParameterBinderController.MinishellParameters seen = (MinishellParameterBinderController.MinishellParameters) 0;
         for (int index = 0; index < args.Count; ++index)
         {
             object obj = args[index];
             if (MinishellParameterBinderController.StartsWith("-command", obj))
             {
                 this.HandleSeenParameter(ref seen, MinishellParameterBinderController.MinishellParameters.Command, "-command");
                 arrayList.Add((object)"-encodedCommand");
                 if (index + 1 >= args.Count)
                 {
                     throw this.NewParameterBindingException((Exception)null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), (Type)null, "NoValueForCommandParameter");
                 }
                 if (!(args[index + 1] is ScriptBlock scriptBlock))
                 {
                     throw this.NewParameterBindingException((Exception)null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), args[index + 1].GetType(), "IncorrectValueForCommandParameter");
                 }
                 string base64String = StringToBase64Converter.StringToBase64String(scriptBlock.ToString());
                 arrayList.Add((object)base64String);
                 ++index;
             }
             else if (obj is ScriptBlock)
             {
                 this.HandleSeenParameter(ref seen, MinishellParameterBinderController.MinishellParameters.Command, "-command");
                 arrayList.Add((object)"-encodedCommand");
                 string base64String = StringToBase64Converter.StringToBase64String(obj.ToString());
                 arrayList.Add((object)base64String);
             }
             else if (MinishellParameterBinderController.StartsWith("-inputFormat", obj))
             {
                 this.HandleSeenParameter(ref seen, MinishellParameterBinderController.MinishellParameters.InputFormat, "-inputFormat");
                 arrayList.Add((object)"-inputFormat");
                 if (index + 1 >= args.Count)
                 {
                     throw this.NewParameterBindingException((Exception)null, ErrorCategory.InvalidArgument, "-inputFormat", typeof(string), (Type)null, "NoValueForInputFormatParameter");
                 }
                 lhs1 = this.ProcessFormatParameterValue("-inputFormat", args[index + 1]);
                 ++index;
                 arrayList.Add((object)lhs1);
             }
             else if (MinishellParameterBinderController.StartsWith("-outputFormat", obj))
             {
                 this.HandleSeenParameter(ref seen, MinishellParameterBinderController.MinishellParameters.OutputFormat, "-outputFormat");
                 arrayList.Add((object)"-outputFormat");
                 if (index + 1 >= args.Count)
                 {
                     throw this.NewParameterBindingException((Exception)null, ErrorCategory.InvalidArgument, "-outputFormat", typeof(string), (Type)null, "NoValueForOutputFormatParameter");
                 }
                 lhs2 = this.ProcessFormatParameterValue("-outputFormat", args[index + 1]);
                 ++index;
                 arrayList.Add((object)lhs2);
             }
             else if (MinishellParameterBinderController.StartsWith("-args", obj))
             {
                 this.HandleSeenParameter(ref seen, MinishellParameterBinderController.MinishellParameters.Arguments, "-args");
                 arrayList.Add((object)"-encodedarguments");
                 if (index + 1 >= args.Count)
                 {
                     throw this.NewParameterBindingException((Exception)null, ErrorCategory.InvalidArgument, "-args", typeof(string), (Type)null, "NoValuesSpecifiedForArgs");
                 }
                 string encodedString = MinishellParameterBinderController.ConvertArgsValueToEncodedString(args[index + 1]);
                 ++index;
                 arrayList.Add((object)encodedString);
             }
             else
             {
                 arrayList.Add(obj);
             }
         }
         if (lhs1 == null)
         {
             arrayList.Add((object)"-inputFormat");
             arrayList.Add((object)"xml");
             lhs1 = "xml";
         }
         if (lhs2 == null)
         {
             arrayList.Add((object)"-outputFormat");
             if (outputRedirected)
             {
                 arrayList.Add((object)"xml");
                 lhs2 = "xml";
             }
             else
             {
                 arrayList.Add((object)"text");
                 lhs2 = "text";
             }
         }
         this.inputFormatValue  = !MinishellParameterBinderController.StartsWith(lhs1, (object)"xml") ? NativeCommandIOFormat.Text : NativeCommandIOFormat.Xml;
         this.outputFormatValue = !MinishellParameterBinderController.StartsWith(lhs2, (object)"xml") ? NativeCommandIOFormat.Text : NativeCommandIOFormat.Xml;
         if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
         {
             this.nonInteractive = true;
             arrayList.Insert(0, (object)"-noninteractive");
         }
         return(arrayList);
     }
 }
 internal MinishellParameterBinderController(NativeCommand command) : base(command)
 {
     this.inputFormatValue = NativeCommandIOFormat.Xml;
 }
        private ArrayList ProcessMinishellParameters(ArrayList args, bool outputRedirected, string hostName)
        {
            ArrayList           list = new ArrayList();
            string              str  = null;
            string              str2 = null;
            MinishellParameters seen = 0;

            for (int i = 0; i < args.Count; i++)
            {
                object obj2 = args[i];
                if (StartsWith("-command", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
                    list.Add("-encodedCommand");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), null, "NoValueForCommandParameter", new object[0]);
                    }
                    ScriptBlock block = args[i + 1] as ScriptBlock;
                    if (block == null)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), args[i + 1].GetType(), "IncorrectValueForCommandParameter", new object[0]);
                    }
                    string str3 = StringToBase64Converter.StringToBase64String(block.ToString());
                    list.Add(str3);
                    i++;
                }
                else if (obj2 is ScriptBlock)
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
                    list.Add("-encodedCommand");
                    string str4 = StringToBase64Converter.StringToBase64String(obj2.ToString());
                    list.Add(str4);
                }
                else if (StartsWith("-inputFormat", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.InputFormat, "-inputFormat");
                    list.Add("-inputFormat");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-inputFormat", typeof(string), null, "NoValueForInputFormatParameter", new object[0]);
                    }
                    str = this.ProcessFormatParameterValue("-inputFormat", args[i + 1]);
                    i++;
                    list.Add(str);
                }
                else if (StartsWith("-outputFormat", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.OutputFormat, "-outputFormat");
                    list.Add("-outputFormat");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-outputFormat", typeof(string), null, "NoValueForOutputFormatParameter", new object[0]);
                    }
                    str2 = this.ProcessFormatParameterValue("-outputFormat", args[i + 1]);
                    i++;
                    list.Add(str2);
                }
                else if (StartsWith("-args", obj2))
                {
                    this.HandleSeenParameter(ref seen, MinishellParameters.Arguments, "-args");
                    list.Add("-encodedarguments");
                    if ((i + 1) >= args.Count)
                    {
                        throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-args", typeof(string), null, "NoValuesSpecifiedForArgs", new object[0]);
                    }
                    string str5 = ConvertArgsValueToEncodedString(args[i + 1]);
                    i++;
                    list.Add(str5);
                }
                else
                {
                    list.Add(obj2);
                }
            }
            if (str == null)
            {
                list.Add("-inputFormat");
                list.Add("xml");
                str = "xml";
            }
            if (str2 == null)
            {
                list.Add("-outputFormat");
                if (outputRedirected)
                {
                    list.Add("xml");
                    str2 = "xml";
                }
                else
                {
                    list.Add("text");
                    str2 = "text";
                }
            }
            if (StartsWith(str, "xml"))
            {
                this.inputFormatValue = NativeCommandIOFormat.Xml;
            }
            else
            {
                this.inputFormatValue = NativeCommandIOFormat.Text;
            }
            if (StartsWith(str2, "xml"))
            {
                this.outputFormatValue = NativeCommandIOFormat.Xml;
            }
            else
            {
                this.outputFormatValue = NativeCommandIOFormat.Text;
            }
            if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
            {
                this.nonInteractive = true;
                list.Insert(0, "-noninteractive");
            }
            return(list);
        }
Exemplo n.º 10
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;
            }
        }
 internal MinishellParameterBinderController(NativeCommand command) : base(command)
 {
     this.inputFormatValue = NativeCommandIOFormat.Xml;
 }
 private ArrayList ProcessMinishellParameters(ArrayList args, bool outputRedirected, string hostName)
 {
     ArrayList list = new ArrayList();
     string str = null;
     string str2 = null;
     MinishellParameters seen = 0;
     for (int i = 0; i < args.Count; i++)
     {
         object obj2 = args[i];
         if (StartsWith("-command", obj2))
         {
             this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
             list.Add("-encodedCommand");
             if ((i + 1) >= args.Count)
             {
                 throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), null, "NoValueForCommandParameter", new object[0]);
             }
             ScriptBlock block = args[i + 1] as ScriptBlock;
             if (block == null)
             {
                 throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-command", typeof(ScriptBlock), args[i + 1].GetType(), "IncorrectValueForCommandParameter", new object[0]);
             }
             string str3 = StringToBase64Converter.StringToBase64String(block.ToString());
             list.Add(str3);
             i++;
         }
         else if (obj2 is ScriptBlock)
         {
             this.HandleSeenParameter(ref seen, MinishellParameters.Command, "-command");
             list.Add("-encodedCommand");
             string str4 = StringToBase64Converter.StringToBase64String(obj2.ToString());
             list.Add(str4);
         }
         else if (StartsWith("-inputFormat", obj2))
         {
             this.HandleSeenParameter(ref seen, MinishellParameters.InputFormat, "-inputFormat");
             list.Add("-inputFormat");
             if ((i + 1) >= args.Count)
             {
                 throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-inputFormat", typeof(string), null, "NoValueForInputFormatParameter", new object[0]);
             }
             str = this.ProcessFormatParameterValue("-inputFormat", args[i + 1]);
             i++;
             list.Add(str);
         }
         else if (StartsWith("-outputFormat", obj2))
         {
             this.HandleSeenParameter(ref seen, MinishellParameters.OutputFormat, "-outputFormat");
             list.Add("-outputFormat");
             if ((i + 1) >= args.Count)
             {
                 throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-outputFormat", typeof(string), null, "NoValueForOutputFormatParameter", new object[0]);
             }
             str2 = this.ProcessFormatParameterValue("-outputFormat", args[i + 1]);
             i++;
             list.Add(str2);
         }
         else if (StartsWith("-args", obj2))
         {
             this.HandleSeenParameter(ref seen, MinishellParameters.Arguments, "-args");
             list.Add("-encodedarguments");
             if ((i + 1) >= args.Count)
             {
                 throw this.NewParameterBindingException(null, ErrorCategory.InvalidArgument, "-args", typeof(string), null, "NoValuesSpecifiedForArgs", new object[0]);
             }
             string str5 = ConvertArgsValueToEncodedString(args[i + 1]);
             i++;
             list.Add(str5);
         }
         else
         {
             list.Add(obj2);
         }
     }
     if (str == null)
     {
         list.Add("-inputFormat");
         list.Add("xml");
         str = "xml";
     }
     if (str2 == null)
     {
         list.Add("-outputFormat");
         if (outputRedirected)
         {
             list.Add("xml");
             str2 = "xml";
         }
         else
         {
             list.Add("text");
             str2 = "text";
         }
     }
     if (StartsWith(str, "xml"))
     {
         this.inputFormatValue = NativeCommandIOFormat.Xml;
     }
     else
     {
         this.inputFormatValue = NativeCommandIOFormat.Text;
     }
     if (StartsWith(str2, "xml"))
     {
         this.outputFormatValue = NativeCommandIOFormat.Xml;
     }
     else
     {
         this.outputFormatValue = NativeCommandIOFormat.Text;
     }
     if (string.IsNullOrEmpty(hostName) || !hostName.Equals("ConsoleHost", StringComparison.OrdinalIgnoreCase))
     {
         this.nonInteractive = true;
         list.Insert(0, "-noninteractive");
     }
     return list;
 }