/// <summary>
        /// Resets the TraceSource flags back to their original value and restores
        /// the original TraceListeners.
        /// </summary>
        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;

                // Reset the flags for the trace switch back to the original value
                ResetTracing(_matchingSources);
                ClearStoredState();
                _matchingSources = null;

                if (_pipeline != null)
                {
                    _pipeline.Dispose();
                    _pipeline = null;
                }

                // If there are any file streams, close those as well.

                if (this.FileStreams != null)
                {
                    foreach (FileStream fileStream in this.FileStreams)
                    {
                        fileStream.Flush();
                        fileStream.Dispose();
                    }
                }
                GC.SuppressFinalize(this);
            }
        } // Dispose
Esempio n. 2
0
        internal static void InvokePipeline(object input, bool ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
        {
            PipelineProcessor pipelineProcessor = new PipelineProcessor();

            System.Management.Automation.ExecutionContext context = funcContext._executionContext;
            Pipe pipe = funcContext._outputPipe;

            try {
                if (context.Events != null)
                {
                    context.Events.ProcessPendingActions();
                }
                if ((input == AutomationNull.Value) && !ignoreInput)
                {
                    AddNoopCommandProcessor(pipelineProcessor, context);
                }
                CommandProcessorBase commandProcessor = null;
                CommandRedirection[] redirections     = null;
                for (int i = 0; i < pipeElements.Length; i++)
                {
                    redirections     = (commandRedirections != null) ? commandRedirections [i] : null;
                    commandProcessor = AddCommand(pipelineProcessor, pipeElements [i], pipeElementAsts [i], redirections, context);
                }
                if ((commandProcessor != null) && !commandProcessor.CommandRuntime.OutputPipe.IsRedirected)
                {
                    pipelineProcessor.LinkPipelineSuccessOutput(pipe ?? new Pipe(new ArrayList()));
                    if (redirections != null)
                    {
                        foreach (CommandRedirection redirection in redirections)
                        {
                            if (redirection is MergingRedirection)
                            {
                                redirection.Bind(pipelineProcessor, commandProcessor, context);
                            }
                        }
                    }
                }
                context.PushPipelineProcessor(pipelineProcessor);
                try {
                    pipelineProcessor.SynchronousExecuteEnumerate(input, null, true);
                } finally {
                    context.PopPipelineProcessor(false);
                }
            }
            finally
            {
                context.QuestionMarkVariableValue = !pipelineProcessor.ExecutionFailed;
                pipelineProcessor.Dispose();
            }
        }
Esempio n. 3
0
        private PipelineProcessor CreatePipelineProcessor()
        {
            CommandCollection commands = this.Commands;

            if (commands == null || commands.Count == 0)
            {
                throw LocalPipeline._trace.NewInvalidOperationException("Runspace", "NoCommandInPipeline");
            }
            PipelineProcessor pipelineProcessor = new PipelineProcessor();

            pipelineProcessor.TopLevel = true;
            bool flag = false;

            try
            {
                foreach (Command command in (Collection <Command>)commands)
                {
                    CommandProcessorBase commandProcessor = command.CreateCommandProcessor(this.LocalRunspace.ExecutionContext, this.LocalRunspace.CommandFactory, this.AddToHistory);
                    commandProcessor.RedirectShellErrorOutputPipe = this.RedirectShellErrorOutputPipe;
                    pipelineProcessor.Add(commandProcessor);
                }
                return(pipelineProcessor);
            }
            catch (RuntimeException ex)
            {
                flag = true;
                throw;
            }
            catch (Exception ex)
            {
                flag = true;
                CommandProcessorBase.CheckForSevereException(ex);
                throw new RuntimeException(ResourceManagerCache.GetResourceString("Pipeline", "CannotCreatePipeline"), ex);
            }
            finally
            {
                if (flag)
                {
                    pipelineProcessor.Dispose();
                }
            }
        }
Esempio n. 4
0
        private void InvokeHelper()
        {
            PipelineProcessor pipelineProcessor = (PipelineProcessor)null;

            try
            {
                this.RaisePipelineStateEvents();
                this.RecordPipelineStartTime();
                try
                {
                    pipelineProcessor = this.CreatePipelineProcessor();
                }
                catch (Exception ex)
                {
                    if (this.SetPipelineSessionState)
                    {
                        this.Runspace.ExecutionContext.AppendDollarError((object)ex);
                    }
                    throw;
                }
                if (this.useExternalInput)
                {
                    pipelineProcessor.ExternalInput = this.InputStream.ObjectReader;
                }
                pipelineProcessor.ExternalSuccessOutput = this.OutputStream.ObjectWriter;
                pipelineProcessor.ExternalErrorOutput   = this.ErrorStream.ObjectWriter;
                this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers(this.InformationalBuffers);
                bool flag = true;
                bool enclosingStatementBlock = this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock;
                this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = false;
                try
                {
                    this._stopper.Push(pipelineProcessor);
                    if (!this.AddToHistory)
                    {
                        flag = this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue;
                        this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = true;
                    }
                    else
                    {
                        this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = false;
                    }
                    if (!this.IsNested)
                    {
                        this.LocalRunspace.ExecutionContext.ResetScopeDepth();
                    }
                    this.LocalRunspace.ExecutionContext.ResetRedirection();
                    try
                    {
                        pipelineProcessor.Execute();
                    }
                    catch (ExitException ex1)
                    {
                        int exitCode = 1;
                        if (this.IsNested)
                        {
                            try
                            {
                                this.LocalRunspace.ExecutionContext.SetVariable("global:LASTEXITCODE", (object)(int)ex1.Argument);
                            }
                            finally
                            {
                                try
                                {
                                    LocalPipeline._trace.WriteLine("exiting nested prompt at top level from exit statement", new object[0]);
                                    this.LocalRunspace.ExecutionContext.EngineHostInterface.ExitNestedPrompt();
                                }
                                catch (ExitNestedPromptException ex2)
                                {
                                }
                            }
                        }
                        else
                        {
                            try
                            {
                                exitCode = (int)ex1.Argument;
                            }
                            finally
                            {
                                this.LocalRunspace.ExecutionContext.EngineHostInterface.SetShouldExit(exitCode);
                            }
                        }
                    }
                    catch (ExitNestedPromptException ex)
                    {
                        LocalPipeline._trace.WriteLine("exiting nested prompt at top level from direct method call", new object[0]);
                    }
                    catch (FlowControlException ex)
                    {
                    }
                }
                finally
                {
                    if (this.LocalRunspace.Events is PSLocalEventManager events)
                    {
                        events.ProcessPendingActions();
                    }
                    this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = enclosingStatementBlock;
                    this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers((PSInformationalBuffers)null);
                    if (!this.AddToHistory)
                    {
                        this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue = flag;
                    }
                    this._stopper.Pop();
                }
            }
            finally
            {
                pipelineProcessor?.Dispose();
            }
        }
Esempio n. 5
0
        private PipelineProcessor CreatePipelineProcessor()
        {
            PipelineProcessor processor2;
            CommandCollection commands = base.Commands;

            if ((commands == null) || (commands.Count == 0))
            {
                throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "NoCommandInPipeline", new object[0]);
            }
            PipelineProcessor processor = new PipelineProcessor {
                TopLevel = true
            };
            bool flag = false;

            try
            {
                foreach (Command command in commands)
                {
                    CommandProcessorBase base2;
                    if (command.CommandInfo == null)
                    {
                        base2 = command.CreateCommandProcessor(this.LocalRunspace.ExecutionContext, this.LocalRunspace.CommandFactory, base.AddToHistory, this.IsNested ? CommandOrigin.Internal : CommandOrigin.Runspace);
                    }
                    else
                    {
                        CmdletInfo commandInfo = (CmdletInfo)command.CommandInfo;
                        base2 = new CommandProcessor(commandInfo, this.LocalRunspace.ExecutionContext);
                        PSSQMAPI.IncrementData(commandInfo.CommandType);
                        base2.Command.CommandOriginInternal       = CommandOrigin.Internal;
                        base2.Command.MyInvocation.InvocationName = commandInfo.Name;
                        if (command.Parameters != null)
                        {
                            foreach (CommandParameter parameter in command.Parameters)
                            {
                                CommandParameterInternal internal2 = CommandParameter.ToCommandParameterInternal(parameter, false);
                                base2.AddParameter(internal2);
                            }
                        }
                    }
                    base2.RedirectShellErrorOutputPipe = base.RedirectShellErrorOutputPipe;
                    processor.Add(base2);
                }
                processor2 = processor;
            }
            catch (RuntimeException)
            {
                flag = true;
                throw;
            }
            catch (Exception exception)
            {
                flag = true;
                CommandProcessorBase.CheckForSevereException(exception);
                throw new RuntimeException(PipelineStrings.CannotCreatePipeline, exception);
            }
            finally
            {
                if (flag)
                {
                    base.SetHadErrors(true);
                    processor.Dispose();
                }
            }
            return(processor2);
        }
Esempio n. 6
0
        private void InvokeHelper()
        {
            PipelineProcessor item = null;

            try
            {
                base.RaisePipelineStateEvents();
                this.RecordPipelineStartTime();
                try
                {
                    item = this.CreatePipelineProcessor();
                }
                catch (Exception exception)
                {
                    if (base.SetPipelineSessionState)
                    {
                        base.SetHadErrors(true);
                        this.Runspace.ExecutionContext.AppendDollarError(exception);
                    }
                    throw;
                }
                if (this.useExternalInput)
                {
                    item.ExternalInput = base.InputStream.ObjectReader;
                }
                item.ExternalSuccessOutput = base.OutputStream.ObjectWriter;
                item.ExternalErrorOutput   = base.ErrorStream.ObjectWriter;
                if (!this.IsChild)
                {
                    this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers(base.InformationalBuffers);
                }
                bool questionMarkVariableValue = true;
                bool exceptionHandlerInEnclosingStatementBlock = this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock;
                this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = false;
                try
                {
                    this._stopper.Push(item);
                    if (!base.AddToHistory)
                    {
                        questionMarkVariableValue = this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue;
                        this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = true;
                    }
                    else
                    {
                        this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = false;
                    }
                    if (!this.IsNested && !base.IsPulsePipeline)
                    {
                        this.LocalRunspace.ExecutionContext.ResetRedirection();
                    }
                    try
                    {
                        item.Execute();
                        base.SetHadErrors(item.ExecutionFailed);
                    }
                    catch (ExitException exception2)
                    {
                        base.SetHadErrors(item.ExecutionFailed);
                        int newValue = 1;
                        if (this.IsNested)
                        {
                            try
                            {
                                newValue = (int)exception2.Argument;
                                this.LocalRunspace.ExecutionContext.SetVariable(SpecialVariables.LastExitCodeVarPath, newValue);
                                return;
                            }
                            finally
                            {
                                try
                                {
                                    this.LocalRunspace.ExecutionContext.EngineHostInterface.ExitNestedPrompt();
                                }
                                catch (ExitNestedPromptException)
                                {
                                }
                            }
                        }
                        try
                        {
                            newValue = (int)exception2.Argument;
                        }
                        finally
                        {
                            this.LocalRunspace.ExecutionContext.EngineHostInterface.SetShouldExit(newValue);
                        }
                    }
                    catch (ExitNestedPromptException)
                    {
                    }
                    catch (FlowControlException)
                    {
                    }
                    catch (Exception)
                    {
                        base.SetHadErrors(true);
                        throw;
                    }
                }
                finally
                {
                    if ((item != null) && (item.Commands != null))
                    {
                        for (int i = 0; i < item.Commands.Count; i++)
                        {
                            CommandProcessorBase base2 = item.Commands[i];
                            EtwActivity.SetActivityId(base2.PipelineActivityId);
                            MshLog.LogCommandLifecycleEvent(base2.Context, CommandState.Terminated, base2.Command.MyInvocation);
                        }
                    }
                    PSLocalEventManager events = this.LocalRunspace.Events as PSLocalEventManager;
                    if (events != null)
                    {
                        events.ProcessPendingActions();
                    }
                    this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = exceptionHandlerInEnclosingStatementBlock;
                    if (!this.IsChild)
                    {
                        this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers(null);
                    }
                    this._stopper.Pop(false);
                    if (!base.AddToHistory)
                    {
                        this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue = questionMarkVariableValue;
                    }
                }
            }
            catch (FlowControlException)
            {
            }
            finally
            {
                if (item != null)
                {
                    item.Dispose();
                    item = null;
                }
            }
        }