protected override void ProcessRecord()
        {
            List <RemoteRunspace>     list       = null;
            List <IThrottleOperation> operations = new List <IThrottleOperation>();
            string parameterSetName = base.ParameterSetName;

            if (parameterSetName != null)
            {
                if (!(parameterSetName == "Session"))
                {
                    if (parameterSetName == "Uri")
                    {
                        list = this.CreateRunspacesWhenUriParameterSpecified();
                        goto Label_0060;
                    }
                    if (parameterSetName == "ComputerName")
                    {
                        list = this.CreateRunspacesWhenComputerNameParameterSpecified();
                        goto Label_0060;
                    }
                }
                else
                {
                    list = this.CreateRunspacesWhenRunspaceParameterSpecified();
                    goto Label_0060;
                }
            }
            list = new List <RemoteRunspace>();
Label_0060:
            foreach (RemoteRunspace runspace in list)
            {
                runspace.Events.ReceivedEvents.PSEventReceived += new PSEventReceivedEventHandler(this.OnRunspacePSEventReceived);
                OpenRunspaceOperation item = new OpenRunspaceOperation(runspace);
                item.OperationComplete          += new EventHandler <OperationStateEventArgs>(this.HandleRunspaceStateChanged);
                runspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
                operations.Add(item);
            }
            this.throttleManager.SubmitOperations(operations);
            this.allOperations.Add(operations);
            foreach (object obj2 in this.stream.ObjectReader.NonBlockingRead())
            {
                base.WriteStreamObject((Action <Cmdlet>)obj2);
            }
        }
Beispiel #2
0
 protected override void ProcessRecord()
 {
     List<RemoteRunspace> list = null;
     List<IThrottleOperation> operations = new List<IThrottleOperation>();
     string parameterSetName = base.ParameterSetName;
     if (parameterSetName != null)
     {
         if (!(parameterSetName == "Session"))
         {
             if (parameterSetName == "Uri")
             {
                 list = this.CreateRunspacesWhenUriParameterSpecified();
                 goto Label_0060;
             }
             if (parameterSetName == "ComputerName")
             {
                 list = this.CreateRunspacesWhenComputerNameParameterSpecified();
                 goto Label_0060;
             }
         }
         else
         {
             list = this.CreateRunspacesWhenRunspaceParameterSpecified();
             goto Label_0060;
         }
     }
     list = new List<RemoteRunspace>();
 Label_0060:
     foreach (RemoteRunspace runspace in list)
     {
         runspace.Events.ReceivedEvents.PSEventReceived += new PSEventReceivedEventHandler(this.OnRunspacePSEventReceived);
         OpenRunspaceOperation item = new OpenRunspaceOperation(runspace);
         item.OperationComplete += new EventHandler<OperationStateEventArgs>(this.HandleRunspaceStateChanged);
         runspace.URIRedirectionReported += new EventHandler<RemoteDataEventArgs<Uri>>(this.HandleURIDirectionReported);
         operations.Add(item);
     }
     this.throttleManager.SubmitOperations(operations);
     this.allOperations.Add(operations);
     foreach (object obj2 in this.stream.ObjectReader.NonBlockingRead())
     {
         base.WriteStreamObject((Action<Cmdlet>) obj2);
     }
 }
        private void HandleRunspaceStateChanged(object sender, OperationStateEventArgs stateEventArgs)
        {
            ErrorRecord errorRecord;
            PSRemotingTransportException exception2;
            string str;

            if (sender == null)
            {
                throw PSTraceSource.NewArgumentNullException("sender");
            }
            if (stateEventArgs == null)
            {
                throw PSTraceSource.NewArgumentNullException("stateEventArgs");
            }
            RunspaceStateEventArgs baseEvent        = stateEventArgs.BaseEvent as RunspaceStateEventArgs;
            RunspaceState          state            = baseEvent.RunspaceStateInfo.State;
            OpenRunspaceOperation  operation        = sender as OpenRunspaceOperation;
            RemoteRunspace         operatedRunspace = operation.OperatedRunspace;

            if (operatedRunspace != null)
            {
                operatedRunspace.URIRedirectionReported -= new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
            }
            PipelineWriter objectWriter = this.stream.ObjectWriter;
            Exception      reason       = baseEvent.RunspaceStateInfo.Reason;

            switch (state)
            {
            case RunspaceState.Opened:
            {
                PSSession remoteRunspaceInfo = new PSSession(operatedRunspace);
                base.RunspaceRepository.Add(remoteRunspaceInfo);
                Action <Cmdlet> action = cmdlet => cmdlet.WriteObject(remoteRunspaceInfo);
                if (objectWriter.IsOpen)
                {
                    objectWriter.Write(action);
                }
                return;
            }

            case RunspaceState.Closed:
            {
                Uri             uri     = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <Uri>(operatedRunspace.ConnectionInfo, "ConnectionUri", null);
                string          message = base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceClosed, new object[] { (uri != null) ? uri.AbsoluteUri : string.Empty });
                Action <Cmdlet> action3 = cmdlet => cmdlet.WriteVerbose(message);
                if (objectWriter.IsOpen)
                {
                    objectWriter.Write(action3);
                }
                if (reason != null)
                {
                    ErrorRecord     errorRecord2 = new ErrorRecord(reason, "PSSessionStateClosed", ErrorCategory.OpenError, operatedRunspace);
                    Action <Cmdlet> action4      = cmdlet => cmdlet.WriteError(errorRecord2);
                    if (objectWriter.IsOpen)
                    {
                        objectWriter.Write(action4);
                    }
                }
                return;
            }

            case RunspaceState.Closing:
                return;

            case RunspaceState.Broken:
                exception2 = reason as PSRemotingTransportException;
                str        = null;
                if (exception2 != null)
                {
                    OpenRunspaceOperation operation2 = sender as OpenRunspaceOperation;
                    if (operation2 != null)
                    {
                        string computerName = operation2.OperatedRunspace.ConnectionInfo.ComputerName;
                        if (exception2.ErrorCode != -2144108135)
                        {
                            str = "[" + computerName + "] ";
                            if (!string.IsNullOrEmpty(exception2.Message))
                            {
                                str = str + exception2.Message;
                            }
                            else if (!string.IsNullOrEmpty(exception2.TransportMessage))
                            {
                                str = str + exception2.TransportMessage;
                            }
                            break;
                        }
                        string str3 = PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.URIRedirectionReported, new object[] { exception2.Message, "MaximumConnectionRedirectionCount", "PSSessionOption", "AllowRedirection" });
                        str = "[" + computerName + "] " + str3;
                    }
                }
                break;

            default:
                return;
            }
            PSRemotingDataStructureException exception3 = reason as PSRemotingDataStructureException;

            if (exception3 != null)
            {
                OpenRunspaceOperation operation3 = sender as OpenRunspaceOperation;
                if (operation3 != null)
                {
                    string str4 = operation3.OperatedRunspace.ConnectionInfo.ComputerName;
                    str = "[" + str4 + "] " + exception3.Message;
                }
            }
            if (reason == null)
            {
                reason = new RuntimeException(base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, new object[] { state }));
            }
            string fQEIDFromTransportError = WSManTransportManagerUtils.GetFQEIDFromTransportError((exception2 != null) ? exception2.ErrorCode : 0, this._defaultFQEID);

            errorRecord = new ErrorRecord(reason, operatedRunspace, fQEIDFromTransportError, ErrorCategory.OpenError, null, null, null, null, null, str, null);
            Action <Cmdlet> action2 = cmdlet => cmdlet.WriteError(errorRecord);

            if (objectWriter.IsOpen)
            {
                objectWriter.Write(action2);
            }
            this.toDispose.Add(operatedRunspace);
        }
        } // BeginProcessing

        /// <summary>
        /// The runspace objects will be created using OpenAsync. 
        /// At the end, the method will check if any runspace
        /// opened has already become available. If so, then it 
        /// will be written to the pipeline
        /// </summary>
        protected override void ProcessRecord()
        {
            List<RemoteRunspace> remoteRunspaces = null;
            List<IThrottleOperation> operations = new List<IThrottleOperation>();

            switch (ParameterSetName)
            {
                case NewPSSessionCommand.SessionParameterSet:
                    {
                        remoteRunspaces = CreateRunspacesWhenRunspaceParameterSpecified();
                    }
                    break;

                case "Uri":
                    {
                        remoteRunspaces = CreateRunspacesWhenUriParameterSpecified();
                    }
                    break;

                case NewPSSessionCommand.ComputerNameParameterSet:
                    {
                        remoteRunspaces = CreateRunspacesWhenComputerNameParameterSpecified();
                    }
                    break;

                case NewPSSessionCommand.VMIdParameterSet:
                case NewPSSessionCommand.VMNameParameterSet:
                    {
                        remoteRunspaces = CreateRunspacesWhenVMParameterSpecified();
                    }
                    break;

                case NewPSSessionCommand.ContainerIdParameterSet:
                    {
                        remoteRunspaces = CreateRunspacesWhenContainerParameterSpecified();
                    }
                    break;

                case NewPSSessionCommand.SSHHostParameterSet:
                    {
                        remoteRunspaces = CreateRunspacesForSSHHostParameterSet();
                    }
                    break;

                default:
                    {
                        Dbg.Assert(false, "Missing paramenter set in switch statement");
                        remoteRunspaces = new List<RemoteRunspace>(); // added to avoid prefast warning
                    }
                    break;
            } // switch (ParameterSetName...

            foreach (RemoteRunspace remoteRunspace in remoteRunspaces)
            {
                remoteRunspace.Events.ReceivedEvents.PSEventReceived += OnRunspacePSEventReceived;

                OpenRunspaceOperation operation = new OpenRunspaceOperation(remoteRunspace);
                // HandleRunspaceStateChanged callback is added before ThrottleManager complete
                // callback handlers so HandleRunspaceStateChanged will always be called first.
                operation.OperationComplete +=
                    new EventHandler<OperationStateEventArgs>(HandleRunspaceStateChanged);
                remoteRunspace.URIRedirectionReported += HandleURIDirectionReported;
                operations.Add(operation);
            }

            // submit list of operations to throttle manager to start opening
            // runspaces
            _throttleManager.SubmitOperations(operations);

            // Add to list for clean up.
            _allOperations.Add(operations);

            // If there are any runspaces opened asynchronously 
            // that are ready now, check their status and do
            // necessary action. If there are any error records
            // or verbose messages write them as well
            Collection<object> streamObjects =
                _stream.ObjectReader.NonBlockingRead();

            foreach (object streamObject in streamObjects)
            {
                WriteStreamObject((Action<Cmdlet>)streamObject);
            } // foreach
        }// ProcessRecord()