Example #1
0
        private void HandleStartEvent(object sender, PSEventArgs args)
        {
            ServerSteppablePipelineDriverEventArg sourceEventArgs = args.SourceEventArgs as ServerSteppablePipelineDriverEventArg;
            ServerSteppablePipelineDriver         steppableDriver = sourceEventArgs.SteppableDriver;
            Exception reason = null;

            try
            {
                using (ExecutionContextForStepping.PrepareExecutionContext(steppableDriver.LocalPowerShell.GetContextFromTLS(), steppableDriver.LocalPowerShell.InformationalBuffers, steppableDriver.RemoteHost))
                {
                    steppableDriver.SteppablePipeline = steppableDriver.LocalPowerShell.GetSteppablePipeline();
                    steppableDriver.SteppablePipeline.Begin(!steppableDriver.NoInput);
                }
                if (steppableDriver.NoInput)
                {
                    steppableDriver.HandleInputEndReceived(this, EventArgs.Empty);
                }
            }
            catch (Exception exception2)
            {
                reason = exception2;
            }
            if (reason != null)
            {
                steppableDriver.SetState(PSInvocationState.Failed, reason);
            }
        }
 internal static ExecutionContextForStepping PrepareExecutionContext(ExecutionContext ctxt, PSInformationalBuffers newBuffers, PSHost newHost)
 {
     ExecutionContextForStepping stepping = new ExecutionContextForStepping(ctxt) {
         originalInformationalBuffers = ctxt.InternalHost.InternalUI.GetInformationalMessageBuffers(),
         originalHost = ctxt.InternalHost.ExternalHost
     };
     ctxt.InternalHost.InternalUI.SetInformationalMessageBuffers(newBuffers);
     ctxt.InternalHost.SetHostRef(newHost);
     return stepping;
 }
        internal static ExecutionContextForStepping PrepareExecutionContext(ExecutionContext ctxt, PSInformationalBuffers newBuffers, PSHost newHost)
        {
            ExecutionContextForStepping stepping = new ExecutionContextForStepping(ctxt)
            {
                originalInformationalBuffers = ctxt.InternalHost.InternalUI.GetInformationalMessageBuffers(),
                originalHost = ctxt.InternalHost.ExternalHost
            };

            ctxt.InternalHost.InternalUI.SetInformationalMessageBuffers(newBuffers);
            ctxt.InternalHost.SetHostRef(newHost);
            return(stepping);
        }
        /// <summary>
        /// Handles the start pipeline event, this is called by the event manager
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void HandleStartEvent(object sender, PSEventArgs args)
        {
            ServerSteppablePipelineDriverEventArg driverArg = (object)args.SourceEventArgs as ServerSteppablePipelineDriverEventArg;
            ServerSteppablePipelineDriver         driver    = driverArg.SteppableDriver;

            Exception exceptionOccurred = null;

            try
            {
                using (ExecutionContextForStepping ctxt =
                           ExecutionContextForStepping.PrepareExecutionContext(
                               driver.LocalPowerShell.GetContextFromTLS(),
                               driver.LocalPowerShell.InformationalBuffers,
                               driver.RemoteHost))
                {
                    driver.SteppablePipeline = driver.LocalPowerShell.GetSteppablePipeline();
                    driver.SteppablePipeline.Begin(!driver.NoInput);
                }

                if (driver.NoInput)
                {
                    driver.HandleInputEndReceived(this, EventArgs.Empty);
                }
            }
            catch (Exception e)
            {
                // We need to catch this so that we can set the pipeline execution;
                // state to "failed" and send the exception as an error to the user.
                // Otherwise, the event manager will swallow this exception and
                // cause the client to hang.
                exceptionOccurred = e;
            }

            if (exceptionOccurred != null)
            {
                driver.SetState(PSInvocationState.Failed, exceptionOccurred);
            }
        }
        /// <summary>
        /// Handles process record event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void HandleProcessRecord(object sender, PSEventArgs args)
        {
            ServerSteppablePipelineDriverEventArg driverArg = (object)args.SourceEventArgs as ServerSteppablePipelineDriverEventArg;
            ServerSteppablePipelineDriver         driver    = driverArg.SteppableDriver;

            lock (driver.SyncObject)
            {
                // Make sure start event handler was called
                if (driver.SteppablePipeline == null)
                {
                    return;
                }

                // make sure only one thread does the processing
                if (driver.ProcessingInput)
                {
                    return;
                }
                driver.ProcessingInput = true;
                driver.Pulsed          = false;
            }

            bool      shouldDoComplete  = false;
            Exception exceptionOccurred = null;

            try
            {
                using (ExecutionContextForStepping ctxt =
                           ExecutionContextForStepping.PrepareExecutionContext(
                               driver.LocalPowerShell.GetContextFromTLS(),
                               driver.LocalPowerShell.InformationalBuffers,
                               driver.RemoteHost))
                {
                    bool isProcessCalled = false;
                    while (true)
                    {
                        if (driver.PipelineState != PSInvocationState.Running)
                        {
                            driver.SetState(driver.PipelineState, null);
                            return;
                        }

                        if (!driver.InputEnumerator.MoveNext())
                        {
                            shouldDoComplete = true;
                            if (!driver.NoInput || isProcessCalled)
                            {
                                // if there is noInput then we
                                // need to call process atleast once
                                break;
                            }
                        }

                        isProcessCalled = true;
                        Array output;
                        if (driver.NoInput)
                        {
                            output = driver.SteppablePipeline.Process();
                        }
                        else
                        {
                            output = driver.SteppablePipeline.Process(driver.InputEnumerator.Current);
                        }
                        foreach (object o in output)
                        {
                            if (driver.PipelineState != PSInvocationState.Running)
                            {
                                driver.SetState(driver.PipelineState, null);
                                return;
                            }

                            // send the output data to the client
                            driver.DataStructureHandler.SendOutputDataToClient(PSObject.AsPSObject(o));
                        }

                        lock (driver.SyncObject)
                        {
                            driver.TotalObjectsProcessed++;
                            if (driver.TotalObjectsProcessed >= driver.Input.Count)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                exceptionOccurred = e;
            }
            finally
            {
                lock (driver.SyncObject)
                {
                    driver.ProcessingInput = false;
                    driver.CheckAndPulseForProcessing(false);
                }
                // Check if should perform stop
                if (driver.PipelineState == PSInvocationState.Stopping)
                {
                    driver.PerformStop();
                }
            }

            if (shouldDoComplete)
            {
                try
                {
                    using (ExecutionContextForStepping ctxt =
                               ExecutionContextForStepping.PrepareExecutionContext(
                                   driver.LocalPowerShell.GetContextFromTLS(),
                                   driver.LocalPowerShell.InformationalBuffers,
                                   driver.RemoteHost))
                    {
                        Array output = driver.SteppablePipeline.End();
                        foreach (object o in output)
                        {
                            if (driver.PipelineState != PSInvocationState.Running)
                            {
                                driver.SetState(driver.PipelineState, null);
                                return;
                            }

                            // send the output data to the client
                            driver.DataStructureHandler.SendOutputDataToClient(PSObject.AsPSObject(o));
                        }

                        driver.SetState(PSInvocationState.Completed, null);
                        return;
                    }
                }
                catch (Exception e)
                {
                    exceptionOccurred = e;
                }
                finally
                {
                    // Check if should perform stop
                    if (driver.PipelineState == PSInvocationState.Stopping)
                    {
                        driver.PerformStop();
                    }
                }
            }

            if (exceptionOccurred != null)
            {
                driver.SetState(PSInvocationState.Failed, exceptionOccurred);
            }
        }
Example #6
0
        private void HandleProcessRecord(object sender, PSEventArgs args)
        {
            ServerSteppablePipelineDriverEventArg sourceEventArgs = args.SourceEventArgs as ServerSteppablePipelineDriverEventArg;
            ServerSteppablePipelineDriver         steppableDriver = sourceEventArgs.SteppableDriver;

            lock (steppableDriver.SyncObject)
            {
                if ((steppableDriver.SteppablePipeline == null) || steppableDriver.ProcessingInput)
                {
                    return;
                }
                steppableDriver.ProcessingInput = true;
                steppableDriver.Pulsed          = false;
            }
            bool      flag   = false;
            Exception reason = null;

            try
            {
                using (ExecutionContextForStepping.PrepareExecutionContext(steppableDriver.LocalPowerShell.GetContextFromTLS(), steppableDriver.LocalPowerShell.InformationalBuffers, steppableDriver.RemoteHost))
                {
                    bool flag2 = false;
Label_0086:
                    if (steppableDriver.PipelineState != PSInvocationState.Running)
                    {
                        steppableDriver.SetState(steppableDriver.PipelineState, null);
                        return;
                    }
                    if (!steppableDriver.InputEnumerator.MoveNext())
                    {
                        flag = true;
                        if (!steppableDriver.NoInput || flag2)
                        {
                            goto Label_0203;
                        }
                    }
                    flag2 = true;
                    Array array = new int[0];
                    if (steppableDriver.NoInput)
                    {
                        array = steppableDriver.SteppablePipeline.Process();
                    }
                    else
                    {
                        array = steppableDriver.SteppablePipeline.Process(steppableDriver.InputEnumerator.Current);
                    }
                    foreach (object obj2 in array)
                    {
                        if (steppableDriver.PipelineState != PSInvocationState.Running)
                        {
                            steppableDriver.SetState(steppableDriver.PipelineState, null);
                            return;
                        }
                        steppableDriver.DataStructureHandler.SendOutputDataToClient(PSObject.AsPSObject(obj2));
                    }
                    lock (steppableDriver.SyncObject)
                    {
                        steppableDriver.TotalObjectsProcessed++;
                        if (steppableDriver.TotalObjectsProcessed < steppableDriver.Input.Count)
                        {
                            goto Label_0086;
                        }
                    }
                }
            }
            catch (Exception exception2)
            {
                CommandProcessorBase.CheckForSevereException(exception2);
                reason = exception2;
            }
            finally
            {
                lock (steppableDriver.SyncObject)
                {
                    steppableDriver.ProcessingInput = false;
                    steppableDriver.CheckAndPulseForProcessing(false);
                }
                if (steppableDriver.PipelineState == PSInvocationState.Stopping)
                {
                    steppableDriver.PerformStop();
                }
            }
Label_0203:
            if (flag)
            {
                try
                {
                    using (ExecutionContextForStepping.PrepareExecutionContext(steppableDriver.LocalPowerShell.GetContextFromTLS(), steppableDriver.LocalPowerShell.InformationalBuffers, steppableDriver.RemoteHost))
                    {
                        foreach (object obj3 in steppableDriver.SteppablePipeline.End())
                        {
                            if (steppableDriver.PipelineState != PSInvocationState.Running)
                            {
                                steppableDriver.SetState(steppableDriver.PipelineState, null);
                                return;
                            }
                            steppableDriver.DataStructureHandler.SendOutputDataToClient(PSObject.AsPSObject(obj3));
                        }
                        steppableDriver.SetState(PSInvocationState.Completed, null);
                        return;
                    }
                }
                catch (Exception exception3)
                {
                    CommandProcessorBase.CheckForSevereException(exception3);
                    reason = exception3;
                }
                finally
                {
                    if (steppableDriver.PipelineState == PSInvocationState.Stopping)
                    {
                        steppableDriver.PerformStop();
                    }
                }
            }
            if (reason != null)
            {
                steppableDriver.SetState(PSInvocationState.Failed, reason);
            }
        }