Esempio n. 1
0
        private void InvokeThreadProc()
        {
            bool     flag            = false;
            Runspace defaultRunspace = Runspace.DefaultRunspace;

            try
            {
                WindowsImpersonationContext impersonationContext = (WindowsImpersonationContext)null;
                try
                {
                    if (this.InvocationSettings != null && this.InvocationSettings.FlowImpersonationPolicy)
                    {
                        impersonationContext = new WindowsIdentity(this.InvocationSettings.WindowsIdentityToImpersonate.Token).Impersonate();
                    }
                    if (this.InvocationSettings != null && this.InvocationSettings.Host != null)
                    {
                        if (this.InvocationSettings.Host is InternalHost host)
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(host.ExternalHost);
                        }
                        else
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(this.InvocationSettings.Host);
                        }
                    }
                    if (this.LocalRunspace.ExecutionContext.InternalHost.ExternalHost.ShouldSetThreadUILanguageToZero)
                    {
                        int num = (int)NativeCultureResolver.SetThreadUILanguage((short)0);
                    }
                    Runspace.DefaultRunspace = (Runspace)this.LocalRunspace;
                    this.InvokeHelper();
                    this.SetPipelineState(PipelineState.Completed);
                }
                finally
                {
                    if (impersonationContext != null)
                    {
                        try
                        {
                            impersonationContext.Undo();
                            impersonationContext.Dispose();
                        }
                        catch (SecurityException ex)
                        {
                            LocalPipeline._trace.TraceWarning("There is some probelm undoing the impersonation context");
                            LocalPipeline._trace.TraceException((Exception)ex);
                        }
                    }
                }
            }
            catch (PipelineStoppedException ex)
            {
                this.SetPipelineState(PipelineState.Stopped, (Exception)ex);
            }
            catch (RuntimeException ex)
            {
                flag = ex is IncompleteParseException;
                this.SetPipelineState(PipelineState.Failed, (Exception)ex);
            }
            catch (ScriptCallDepthException ex)
            {
                this.SetPipelineState(PipelineState.Failed, (Exception)ex);
            }
            catch (SecurityException ex)
            {
                this.SetPipelineState(PipelineState.Failed, (Exception)ex);
            }
            catch (ThreadAbortException ex)
            {
                this.SetPipelineState(PipelineState.Failed, (Exception)ex);
            }
            catch (HaltCommandException ex)
            {
                this.SetPipelineState(PipelineState.Completed);
            }
            finally
            {
                if (this.LocalRunspace.ExecutionContext.InternalHost.IsHostRefSet)
                {
                    this.LocalRunspace.ExecutionContext.InternalHost.RevertHostRef();
                }
                Runspace.DefaultRunspace = defaultRunspace;
                if (!flag)
                {
                    try
                    {
                        bool inBreakpoint = this.LocalRunspace.ExecutionContext.Debugger.InBreakpoint;
                        if (this._historyIdForThisPipeline == -1L)
                        {
                            this.AddHistoryEntry(inBreakpoint);
                        }
                        else
                        {
                            this.UpdateHistoryEntryAddedByAddHistoryCmdlet(inBreakpoint);
                        }
                    }
                    catch (TerminateException ex)
                    {
                    }
                }
                if (this.OutputStream.IsOpen)
                {
                    try
                    {
                        this.OutputStream.Close();
                    }
                    catch (ObjectDisposedException ex)
                    {
                        LocalPipeline._trace.TraceException((Exception)ex);
                    }
                }
                if (this.ErrorStream.IsOpen)
                {
                    try
                    {
                        this.ErrorStream.Close();
                    }
                    catch (ObjectDisposedException ex)
                    {
                        LocalPipeline._trace.TraceException((Exception)ex);
                    }
                }
                if (this.InputStream.IsOpen)
                {
                    try
                    {
                        this.InputStream.Close();
                    }
                    catch (ObjectDisposedException ex)
                    {
                        LocalPipeline._trace.TraceException((Exception)ex);
                    }
                }
                this.LocalRunspace.RemoveFromRunningPipelineList((PipelineBase)this);
                if (!this.SyncInvokeCall)
                {
                    this.RaisePipelineStateEvents();
                }
            }
        }
Esempio n. 2
0
        private void InvokeThreadProc()
        {
            bool     flag            = false;
            Runspace defaultRunspace = Runspace.DefaultRunspace;

            try
            {
                WindowsImpersonationContext context = null;
                try
                {
                    if ((base.InvocationSettings != null) && base.InvocationSettings.FlowImpersonationPolicy)
                    {
                        context = new WindowsIdentity(base.InvocationSettings.WindowsIdentityToImpersonate.Token).Impersonate();
                    }
                    if ((base.InvocationSettings != null) && (base.InvocationSettings.Host != null))
                    {
                        InternalHost host = base.InvocationSettings.Host as InternalHost;
                        if (host != null)
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(host.ExternalHost);
                        }
                        else
                        {
                            this.LocalRunspace.ExecutionContext.InternalHost.SetHostRef(base.InvocationSettings.Host);
                        }
                    }
                    if (this.LocalRunspace.ExecutionContext.InternalHost.ExternalHost.ShouldSetThreadUILanguageToZero)
                    {
                        NativeCultureResolver.SetThreadUILanguage(0);
                    }
                    Runspace.DefaultRunspace = this.LocalRunspace;
                    this.InvokeHelper();
                    base.SetPipelineState(PipelineState.Completed);
                }
                finally
                {
                    if (context != null)
                    {
                        try
                        {
                            context.Undo();
                            context.Dispose();
                            context = null;
                        }
                        catch (SecurityException)
                        {
                        }
                    }
                }
            }
            catch (PipelineStoppedException exception)
            {
                base.SetPipelineState(PipelineState.Stopped, exception);
            }
            catch (RuntimeException exception2)
            {
                flag = exception2 is IncompleteParseException;
                base.SetPipelineState(PipelineState.Failed, exception2);
                base.SetHadErrors(true);
            }
            catch (ScriptCallDepthException exception3)
            {
                base.SetPipelineState(PipelineState.Failed, exception3);
                base.SetHadErrors(true);
            }
            catch (SecurityException exception4)
            {
                base.SetPipelineState(PipelineState.Failed, exception4);
                base.SetHadErrors(true);
            }
            catch (ThreadAbortException exception5)
            {
                base.SetPipelineState(PipelineState.Failed, exception5);
                base.SetHadErrors(true);
            }
            catch (HaltCommandException)
            {
                base.SetPipelineState(PipelineState.Completed);
            }
            finally
            {
                if (((base.InvocationSettings != null) && (base.InvocationSettings.Host != null)) && this.LocalRunspace.ExecutionContext.InternalHost.IsHostRefSet)
                {
                    this.LocalRunspace.ExecutionContext.InternalHost.RevertHostRef();
                }
                Runspace.DefaultRunspace = defaultRunspace;
                if (!flag)
                {
                    try
                    {
                        bool inBreakpoint = this.LocalRunspace.ExecutionContext.Debugger.InBreakpoint;
                        if (this._historyIdForThisPipeline == -1L)
                        {
                            this.AddHistoryEntry(inBreakpoint);
                        }
                        else
                        {
                            this.UpdateHistoryEntryAddedByAddHistoryCmdlet(inBreakpoint);
                        }
                    }
                    catch (TerminateException)
                    {
                    }
                }
                if (base.OutputStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.OutputStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                if (base.ErrorStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.ErrorStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                if (base.InputStream.IsOpen && !this.IsChild)
                {
                    try
                    {
                        base.InputStream.Close();
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                }
                this.ClearStreams();
                this.LocalRunspace.RemoveFromRunningPipelineList(this);
                if (!base.SyncInvokeCall)
                {
                    base.RaisePipelineStateEvents();
                }
            }
        }