Пример #1
0
 internal void Invoke(PSEventSubscriber eventSubscriber, PSEventArgs eventArgs)
 {
     if (!base.IsFinishedState(base.JobStateInfo.State))
     {
         base.SetJobState(JobState.Running);
         SessionState publicSessionState = this.action.SessionStateInternal.PublicSessionState;
         publicSessionState.PSVariable.Set("eventSubscriber", eventSubscriber);
         publicSessionState.PSVariable.Set("event", eventArgs);
         publicSessionState.PSVariable.Set("sender", eventArgs.Sender);
         publicSessionState.PSVariable.Set("eventArgs", eventArgs.SourceEventArgs);
         ArrayList resultList = new ArrayList();
         try
         {
             Pipe outputPipe = new Pipe(resultList);
             this.action.InvokeWithPipe(false, System.Management.Automation.ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, AutomationNull.Value, AutomationNull.Value, AutomationNull.Value, outputPipe, null, eventArgs.SourceArgs);
         }
         catch (Exception exception)
         {
             CommandProcessorBase.CheckForSevereException(exception);
             if (!(exception is PipelineStoppedException))
             {
                 this.LogErrorsAndOutput(resultList, publicSessionState);
                 base.SetJobState(JobState.Failed);
             }
             throw;
         }
         this.LogErrorsAndOutput(resultList, publicSessionState);
         this.moreData = true;
     }
 }
Пример #2
0
        protected internal override void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionWhenInCurrentThread)
        {
            WaitCallback callBack = null;

            if (processInCurrentThread)
            {
                this.ProcessNewEventImplementation(newEvent, true);
                ManualResetEventSlim eventProcessed = newEvent.EventProcessed;
                if (eventProcessed != null)
                {
                    while (waitForCompletionWhenInCurrentThread && !eventProcessed.Wait(250))
                    {
                        this.ProcessPendingActions();
                    }
                    eventProcessed.Dispose();
                }
            }
            else
            {
                if (callBack == null)
                {
                    callBack = unused => this.ProcessNewEventImplementation(newEvent, false);
                }
                ThreadPool.QueueUserWorkItem(callBack);
            }
        }
Пример #3
0
        internal PSEventArgs GenerateEvent(string sourceIdentifier, object sender, object[] args, PSObject extraData, bool processInCurrentThread, bool waitForCompletionInCurrentThread = false)
        {
            PSEventArgs newEvent = this.CreateEvent(sourceIdentifier, sender, args, extraData);

            this.ProcessNewEvent(newEvent, processInCurrentThread, waitForCompletionInCurrentThread);
            return(newEvent);
        }
Пример #4
0
 private void HandleRunspacePoolForwardEvent(object sender, PSEventArgs e)
 {
     if (e.ForwardEvent)
     {
         this.dsHandler.SendPSEventArgsToClient(e);
     }
 }
Пример #5
0
        internal void Invoke(PSEventSubscriber eventSubscriber, PSEventArgs eventArgs)
        {
            if (this.IsFinishedState(this.JobStateInfo.State))
            {
                return;
            }
            this.SetJobState(JobState.Running);
            SessionState publicSessionState = this.action.SessionStateInternal.PublicSessionState;

            publicSessionState.PSVariable.Set(nameof(eventSubscriber), (object)eventSubscriber);
            publicSessionState.PSVariable.Set("event", (object)eventArgs);
            publicSessionState.PSVariable.Set("sender", eventArgs.Sender);
            publicSessionState.PSVariable.Set(nameof(eventArgs), (object)eventArgs.SourceEventArgs);
            ArrayList resultList = new ArrayList();

            try
            {
                this.action.InvokeWithPipe(false, false, (object)AutomationNull.Value, (object)AutomationNull.Value, (object)AutomationNull.Value, (Pipe)null, ref resultList, eventArgs.SourceArgs);
            }
            catch (Exception ex)
            {
                CommandProcessorBase.CheckForSevereException(ex);
                if (!(ex is PipelineStoppedException))
                {
                    this.LogErrorsAndOutput(resultList, publicSessionState);
                    this.SetJobState(JobState.Failed);
                }
                throw;
            }
            this.LogErrorsAndOutput(resultList, publicSessionState);
            this.moreData = true;
        }
Пример #6
0
 internal void Invoke(PSEventSubscriber eventSubscriber, PSEventArgs eventArgs)
 {
     if (!base.IsFinishedState(base.JobStateInfo.State))
     {
         base.SetJobState(JobState.Running);
         SessionState publicSessionState = this.action.SessionStateInternal.PublicSessionState;
         publicSessionState.PSVariable.Set("eventSubscriber", eventSubscriber);
         publicSessionState.PSVariable.Set("event", eventArgs);
         publicSessionState.PSVariable.Set("sender", eventArgs.Sender);
         publicSessionState.PSVariable.Set("eventArgs", eventArgs.SourceEventArgs);
         ArrayList resultList = new ArrayList();
         try
         {
             Pipe outputPipe = new Pipe(resultList);
             this.action.InvokeWithPipe(false, System.Management.Automation.ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, AutomationNull.Value, AutomationNull.Value, AutomationNull.Value, outputPipe, null, eventArgs.SourceArgs);
         }
         catch (Exception exception)
         {
             CommandProcessorBase.CheckForSevereException(exception);
             if (!(exception is PipelineStoppedException))
             {
                 this.LogErrorsAndOutput(resultList, publicSessionState);
                 base.SetJobState(JobState.Failed);
             }
             throw;
         }
         this.LogErrorsAndOutput(resultList, publicSessionState);
         this.moreData = true;
     }
 }
Пример #7
0
        private void ProcessNewEventImplementation(PSEventArgs newEvent)
        {
            bool flag = false;

            foreach (PSEventSubscriber eventSubscriber in this.GetEventSubscribers(newEvent.SourceIdentifier))
            {
                newEvent.ForwardEvent = eventSubscriber.ForwardEvent;
                if (eventSubscriber.Action != null)
                {
                    this.AddAction(new EventAction(eventSubscriber, newEvent));
                    flag = true;
                }
                else if (eventSubscriber.HandlerDelegate != null)
                {
                    eventSubscriber.HandlerDelegate(newEvent.Sender, newEvent);
                    flag = true;
                }
            }
            if (flag)
            {
                return;
            }
            if (newEvent.ForwardEvent)
            {
                this.OnForwardEvent(newEvent);
            }
            else
            {
                lock (this.ReceivedEvents.SyncRoot)
                    this.ReceivedEvents.Add(newEvent);
            }
        }
Пример #8
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);
            }
        }
Пример #9
0
 protected virtual void OnForwardEvent(PSEventArgs e)
 {
     EventHandler<PSEventArgs> forwardEvent = this.ForwardEvent;
     if (forwardEvent != null)
     {
         forwardEvent(this, e);
     }
 }
Пример #10
0
        protected virtual void OnForwardEvent(PSEventArgs e)
        {
            EventHandler <PSEventArgs> forwardEvent = this.ForwardEvent;

            if (forwardEvent != null)
            {
                forwardEvent(this, e);
            }
        }
Пример #11
0
        protected virtual void OnForwardEvent(PSEventArgs e)
        {
            EventHandler <PSEventArgs> forwardEvent = this.ForwardEvent;

            if (forwardEvent == null)
            {
                return;
            }
            forwardEvent((object)this, e);
        }
Пример #12
0
        private void ProcessNewEventImplementation(PSEventArgs newEvent, bool processSynchronously)
        {
            bool flag = false;
            List <PSEventSubscriber> list  = new List <PSEventSubscriber>();
            List <PSEventSubscriber> list2 = new List <PSEventSubscriber>();

            foreach (PSEventSubscriber subscriber in this.GetEventSubscribers(newEvent.SourceIdentifier, true))
            {
                newEvent.ForwardEvent = subscriber.ForwardEvent;
                if (subscriber.Action != null)
                {
                    this.AddAction(new EventAction(subscriber, newEvent), processSynchronously);
                    flag = true;
                }
                else if (subscriber.HandlerDelegate != null)
                {
                    if (subscriber.ShouldProcessInExecutionThread)
                    {
                        this.AddAction(new EventAction(subscriber, newEvent), processSynchronously);
                    }
                    else
                    {
                        list.Add(subscriber);
                    }
                    flag = true;
                }
                else
                {
                    list2.Add(subscriber);
                }
            }
            foreach (PSEventSubscriber subscriber2 in list)
            {
                subscriber2.HandlerDelegate(newEvent.Sender, newEvent);
                this.AutoUnregisterEventIfNecessary(subscriber2);
            }
            if (!flag)
            {
                if (newEvent.ForwardEvent)
                {
                    this.OnForwardEvent(newEvent);
                }
                else
                {
                    lock (base.ReceivedEvents.SyncRoot)
                    {
                        base.ReceivedEvents.Add(newEvent);
                    }
                }
                foreach (PSEventSubscriber subscriber3 in list2)
                {
                    this.AutoUnregisterEventIfNecessary(subscriber3);
                }
            }
        }
 private void HandleRunspacePoolForwardEvent(object sender, PSEventArgs e)
 {
     using (ServerRunspacePoolDriver.tracer.TraceMethod())
     {
         if (!e.ForwardEvent)
         {
             return;
         }
         this.dsHandler.SendPSEventArgsToClient(e);
     }
 }
Пример #14
0
 private void ReceivedEvents_PSEventReceived(object sender, PSEventArgs e)
 {
     if (this.sourceIdentifier == null)
     {
         this.NotifyEvent(e);
     }
     else
     {
         this.ScanEventQueue();
     }
 }
Пример #15
0
 internal override void AddForwardedEvent(PSEventArgs forwardedEvent)
 {
     forwardedEvent.EventIdentifier = this.GetNextEventId();
     forwardedEvent.ForwardEvent    = false;
     if (forwardedEvent.ComputerName == null || forwardedEvent.ComputerName.Length == 0)
     {
         forwardedEvent.ComputerName = this.computerName;
         forwardedEvent.RunspaceId   = this.runspaceId;
     }
     this.ProcessNewEvent(forwardedEvent, false);
 }
Пример #16
0
 protected override void ProcessNewEvent(PSEventArgs newEvent, bool processSynchronously)
 {
     if (processSynchronously)
     {
         this.ProcessNewEventImplementation(newEvent);
     }
     else
     {
         ThreadPool.QueueUserWorkItem((WaitCallback)(unused => this.ProcessNewEventImplementation(newEvent)));
     }
 }
Пример #17
0
 internal override void AddForwardedEvent(PSEventArgs forwardedEvent)
 {
     forwardedEvent.EventIdentifier = base.GetNextEventId();
     forwardedEvent.ForwardEvent = false;
     if ((forwardedEvent.ComputerName == null) || (forwardedEvent.ComputerName.Length == 0))
     {
         forwardedEvent.ComputerName = this.computerName;
         forwardedEvent.RunspaceId = this.runspaceId;
     }
     this.ProcessNewEvent(forwardedEvent, false);
 }
Пример #18
0
        internal PSEventArgs GenerateEvent(
            string sourceIdentifier,
            object sender,
            object[] args,
            PSObject extraData,
            bool processSynchronously)
        {
            PSEventArgs newEvent = this.CreateEvent(sourceIdentifier, sender, args, extraData);

            this.ProcessNewEvent(newEvent, processSynchronously);
            return(newEvent);
        }
Пример #19
0
 protected internal override void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionInCurrentThread)
 {
     lock (base.ReceivedEvents.SyncRoot)
     {
         if (newEvent.ForwardEvent)
         {
             this.OnForwardEvent(newEvent);
         }
         else
         {
             base.ReceivedEvents.Add(newEvent);
         }
     }
 }
Пример #20
0
 protected internal override void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionInCurrentThread)
 {
     lock (base.ReceivedEvents.SyncRoot)
     {
         if (newEvent.ForwardEvent)
         {
             this.OnForwardEvent(newEvent);
         }
         else
         {
             base.ReceivedEvents.Add(newEvent);
         }
     }
 }
Пример #21
0
 protected override void ProcessNewEvent(PSEventArgs newEvent, bool processSynchronously)
 {
     lock (this.ReceivedEvents.SyncRoot)
     {
         if (newEvent.ForwardEvent)
         {
             this.OnForwardEvent(newEvent);
         }
         else
         {
             this.ReceivedEvents.Add(newEvent);
         }
     }
 }
Пример #22
0
 private void NotifyEvent(PSEventArgs e)
 {
     if (this.receivedEvent == null)
     {
         lock (this.receivedEventLock)
         {
             if (this.receivedEvent == null)
             {
                 this.receivedEvent = e;
                 this.eventArrived.Set();
             }
         }
     }
 }
Пример #23
0
 internal static RemoteDataObject GeneratePSEventArgs(
     Guid clientRunspacePoolId,
     PSEventArgs e)
 {
     using (RemotingEncoder._trace.TraceMethod())
     {
         PSObject emptyPsObject = RemotingEncoder.CreateEmptyPSObject();
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.EventIdentifier", (object)e.EventIdentifier));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.SourceIdentifier", (object)e.SourceIdentifier));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.TimeGenerated", (object)e.TimeGenerated));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.Sender", e.Sender));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.SourceArgs", (object)e.SourceArgs));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.MessageData", (object)e.MessageData));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.ComputerName", (object)e.ComputerName));
         emptyPsObject.Properties.Add((PSPropertyInfo) new PSNoteProperty("PSEventArgs.RunspaceId", (object)e.RunspaceId));
         return(RemoteDataObject.CreateFrom(RemotingDestination.Client, RemotingDataType.PSEventArgs, clientRunspacePoolId, Guid.Empty, (object)emptyPsObject));
     }
 }
        /// <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 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);
            }
        }
Пример #26
0
 private void OnGetMergedCommandParameterMetadataSafelyEventHandler(object sender, PSEventArgs args)
 {
     var eventArgs = args.SourceEventArgs as GetMergedCommandParameterMetadataSafelyEventArgs;
     if (eventArgs != null)
     {
         try
         {
             // Save the result in our event args as the return value.
             GetMergedCommandParameterMetadata(out eventArgs.Result);
         }
         catch (Exception e)
         {
             // Save the exception so we can throw it on the correct thread.
             eventArgs.Exception = ExceptionDispatchInfo.Capture(e);
         }
     }
 }
        /// <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);
            }
        }
Пример #28
0
 private void OnRunspacePSEventReceived(object sender, PSEventArgs e)
 {
     if (base.Events != null)
     {
         base.Events.AddForwardedEvent(e);
     }
 }
Пример #29
0
        /// <summary>
        /// Send a message with the PSEventArgs to the client.
        /// </summary>
        /// <param name="e">Event to send.</param>
        internal void SendPSEventArgsToClient(PSEventArgs e)
        {
            RemoteDataObject data = RemotingEncoder.GeneratePSEventArgs(_clientRunspacePoolId, e);

            SendDataAsync(data);
        }
Пример #30
0
 private void OnRunspaceForwardEvent(object sender, PSEventArgs e)
 {
     if (e.ForwardEvent)
     {
         this.OnForwardEvent(e);
     }
 }
Пример #31
0
 // Notify that an event has arrived
 private void NotifyEvent(PSEventArgs e)
 {
     if (_receivedEvent == null)
     {
         lock (_receivedEventLock)
         {
             if (_receivedEvent == null)
             {
                 _receivedEvent = e;
                 _eventArrived.Set();
             }
         }
     }
 }
Пример #32
0
 /// <summary>
 /// handler to the runspace pool psevents
 /// </summary>
 private void HandleRunspacePoolForwardEvent(object sender, PSEventArgs e)
 {
     if (e.ForwardEvent)
     {
         DataStructureHandler.SendPSEventArgsToClient(e);
     }
 }
 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);
     }
 }
 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);
     }
 }
Пример #35
0
 public EventAction(PSEventSubscriber sender, PSEventArgs args)
 {
     this.sender = sender;
     this.args = args;
 }
Пример #36
0
        /// <summary>
        /// Handler for the PSEventReceived event; note that this method will be invoked when the remote
        /// computer generates a Windows PowerShell event.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnPSEventReceived(object sender, PSEventArgs e)
        {
            // The information about the original event (in this case FileSystemWatcher.Created) is stored
            // in the SourceArgs property of the PSEventArgs argument of this method.
            //
            // PSEventArgs[0] will be the original event sender (in this case the remote instance of 
            // System.IO.FileSystemWatcher) and PSEventArgs[1] will be the original arguments to the event 
            // (in this case an instance of System.IO.FileSystemEventArgs). Note that these two
            // objects originated on the remote computer and the local computer receives deserialized
            // representations of the objects. The deserialized objects are of type PSObject.
            PSObject fileSystemEventArgs = (PSObject)e.SourceArgs[1];

            // For the purposes of this sample, we only output a message indicating that a file was created
            // on the remote machine, and the time it was created. Since the PSObject is only an instance
            // of a deserialized FileSystemEventArgs object, we need to use the Properties collection to 
            // access the name of the file (i.e. FileSystemEventArgs.Name).
            string fileName = (string)fileSystemEventArgs.Properties["Name"].Value;

            Console.WriteLine("File '{0}' was created on computer '{1}' at {2}", fileName, e.ComputerName, e.TimeGenerated);

            // Increment the count of events received.  
            Events02.eventsReceived++;
        }
Пример #37
0
 protected internal override void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionWhenInCurrentThread)
 {
     WaitCallback callBack = null;
     if (processInCurrentThread)
     {
         this.ProcessNewEventImplementation(newEvent, true);
         ManualResetEventSlim eventProcessed = newEvent.EventProcessed;
         if (eventProcessed != null)
         {
             while (waitForCompletionWhenInCurrentThread && !eventProcessed.Wait(250))
             {
                 this.ProcessPendingActions();
             }
             eventProcessed.Dispose();
         }
     }
     else
     {
         if (callBack == null)
         {
             callBack = unused => this.ProcessNewEventImplementation(newEvent, false);
         }
         ThreadPool.QueueUserWorkItem(callBack);
     }
 }
Пример #38
0
 protected override void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread)
 {
     this.ProcessNewEvent(newEvent, processInCurrentThread, false);
 }
Пример #39
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);
            }
        }
Пример #40
0
 internal abstract void AddForwardedEvent(PSEventArgs forwardedEvent);
Пример #41
0
 private static void OnScriptBlockInvokeEventHandler(object sender, PSEventArgs args)
 {
     ScriptBlockInvocationEventArgs sourceEventArgs = args.SourceEventArgs as ScriptBlockInvocationEventArgs;
     try
     {
         ScriptBlock scriptBlock = sourceEventArgs.ScriptBlock;
         scriptBlock.InvokeWithPipeImpl(sourceEventArgs.UseLocalScope, sourceEventArgs.ErrorHandlingBehavior, sourceEventArgs.DollarUnder, sourceEventArgs.Input, sourceEventArgs.ScriptThis, sourceEventArgs.OutputPipe, sourceEventArgs.InvocationInfo, sourceEventArgs.Args);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         sourceEventArgs.Exception = exception;
     }
 }
Пример #42
0
 internal abstract void AddForwardedEvent(PSEventArgs forwardedEvent);
Пример #43
0
 private void ReceivedEvents_PSEventReceived(Object sender, PSEventArgs e)
 {
     // If they want to wait on just any event
     if (_sourceIdentifier == null)
     {
         NotifyEvent(e);
     }
     // They are waiting on a specific one
     else
     {
         ScanEventQueue();
     }
 }
Пример #44
0
 protected internal virtual void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionWhenInCurrentThread)
 {
     throw new NotImplementedException();
 }
Пример #45
0
 private void OnInternalPoolForwardEvent(object sender, PSEventArgs e)
 {
     this.OnEventForwarded(e);
 }
Пример #46
0
 protected abstract void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread);
Пример #47
0
 private void LocalPowerShell_RunspaceAssigned(object sender, PSEventArgs <Runspace> e)
 {
     _rsUsedToInvokePowerShell = e.Args;
 }
Пример #48
0
 protected internal virtual void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread, bool waitForCompletionWhenInCurrentThread)
 {
     throw new NotImplementedException();
 }
Пример #49
0
 internal void SendPSEventArgsToClient(PSEventArgs e)
 {
     using (ServerRunspacePoolDataStructureHandler.tracer.TraceMethod())
         this.SendDataAsync(RemotingEncoder.GeneratePSEventArgs(this.clientRunspacePoolId, e));
 }
Пример #50
0
 /// <summary>
 /// Adds forwarded events to the local queue
 /// </summary>
 internal void OnRunspacePSEventReceived(object sender, PSEventArgs e)
 {
     if (this.Events != null)
         this.Events.AddForwardedEvent(e);
 }
Пример #51
0
 public EventAction(PSEventSubscriber sender, PSEventArgs args)
 {
     this.sender = sender;
     this.args   = args;
 }
Пример #52
0
 internal override void AddForwardedEvent(PSEventArgs forwardedEvent)
 {
     forwardedEvent.EventIdentifier = this.GetNextEventId();
     this.ProcessNewEvent(forwardedEvent, false);
 }
Пример #53
0
 protected abstract void ProcessNewEvent(PSEventArgs newEvent, bool processInCurrentThread);
Пример #54
0
 private void OnEventForwarded(PSEventArgs e)
 {
     EventHandler<PSEventArgs> internalForwardEvent = this.InternalForwardEvent;
     if (internalForwardEvent != null)
     {
         internalForwardEvent(this, e);
     }
 }
 internal void SendPSEventArgsToClient(PSEventArgs e)
 {
     RemoteDataObject data = RemotingEncoder.GeneratePSEventArgs(this.clientRunspacePoolId, e);
     this.SendDataAsync(data);
 }
 private void LocalPowerShell_RunspaceAssigned(object sender, PSEventArgs<Runspace> e)
 {
     this.rsUsedToInvokePowerShell = e.Args;
 }
Пример #57
0
 private void ProcessNewEventImplementation(PSEventArgs newEvent, bool processSynchronously)
 {
     bool flag = false;
     List<PSEventSubscriber> list = new List<PSEventSubscriber>();
     List<PSEventSubscriber> list2 = new List<PSEventSubscriber>();
     foreach (PSEventSubscriber subscriber in this.GetEventSubscribers(newEvent.SourceIdentifier, true))
     {
         newEvent.ForwardEvent = subscriber.ForwardEvent;
         if (subscriber.Action != null)
         {
             this.AddAction(new EventAction(subscriber, newEvent), processSynchronously);
             flag = true;
         }
         else if (subscriber.HandlerDelegate != null)
         {
             if (subscriber.ShouldProcessInExecutionThread)
             {
                 this.AddAction(new EventAction(subscriber, newEvent), processSynchronously);
             }
             else
             {
                 list.Add(subscriber);
             }
             flag = true;
         }
         else
         {
             list2.Add(subscriber);
         }
     }
     foreach (PSEventSubscriber subscriber2 in list)
     {
         subscriber2.HandlerDelegate(newEvent.Sender, newEvent);
         this.AutoUnregisterEventIfNecessary(subscriber2);
     }
     if (!flag)
     {
         if (newEvent.ForwardEvent)
         {
             this.OnForwardEvent(newEvent);
         }
         else
         {
             lock (base.ReceivedEvents.SyncRoot)
             {
                 base.ReceivedEvents.Add(newEvent);
             }
         }
         foreach (PSEventSubscriber subscriber3 in list2)
         {
             this.AutoUnregisterEventIfNecessary(subscriber3);
         }
     }
 }
Пример #58
0
        internal static RemoteDataObject GeneratePSEventArgs(Guid clientRunspacePoolId, PSEventArgs e)
        {
            PSObject data = CreateEmptyPSObject();

            data.Properties.Add(new PSNoteProperty("PSEventArgs.EventIdentifier", e.EventIdentifier));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.SourceIdentifier", e.SourceIdentifier));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.TimeGenerated", e.TimeGenerated));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.Sender", e.Sender));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.SourceArgs", e.SourceArgs));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.MessageData", e.MessageData));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.ComputerName", e.ComputerName));
            data.Properties.Add(new PSNoteProperty("PSEventArgs.RunspaceId", e.RunspaceId));
            return(RemoteDataObject.CreateFrom(RemotingDestination.InvalidDestination | RemotingDestination.Client, RemotingDataType.PSEventArgs, clientRunspacePoolId, Guid.Empty, data));
        }
Пример #59
0
        private void OnGetMergedCommandParameterMetadataSafelyEventHandler(object sender, PSEventArgs args)
        {
            var eventArgs = args.SourceEventArgs as GetMergedCommandParameterMetadataSafelyEventArgs;

            if (eventArgs != null)
            {
                try
                {
                    // Save the result in our event args as the return value.
                    GetMergedCommandParameterMetadata(out eventArgs.Result);
                }
                catch (Exception e)
                {
                    // Save the exception so we can throw it on the correct thread.
                    eventArgs.Exception = ExceptionDispatchInfo.Capture(e);
                }
            }
        }
Пример #60
0
 internal override void AddForwardedEvent(PSEventArgs forwardedEvent)
 {
     forwardedEvent.EventIdentifier = base.GetNextEventId();
     this.ProcessNewEvent(forwardedEvent, false);
 }