Ejemplo n.º 1
0
        /// <summary>
        /// Perform runspace disconnect processing on all input.
        /// </summary>
        protected override void ProcessRecord()
        {
            Dictionary <Guid, PSSession> psSessions;
            List <IThrottleOperation>    disconnectOperations = new List <IThrottleOperation>();

            try
            {
                // Get all remote runspaces to disconnect.
                if (ParameterSetName == DisconnectPSSessionCommand.SessionParameterSet)
                {
                    if (Session == null || Session.Length == 0)
                    {
                        return;
                    }

                    psSessions = new Dictionary <Guid, PSSession>();
                    foreach (PSSession psSession in Session)
                    {
                        psSessions.Add(psSession.InstanceId, psSession);
                    }
                }
                else
                {
                    psSessions = GetMatchingRunspaces(false, true);
                }

                // Look for local sessions that have the EnableNetworkAccess property set and
                // return a string containing all of the session names.  Emit a warning for
                // these sessions.
                string cnNames = GetLocalhostWithNetworkAccessEnabled(psSessions);
                if (!string.IsNullOrEmpty(cnNames))
                {
                    WriteWarning(
                        StringUtil.Format(RemotingErrorIdStrings.EnableNetworkAccessWarning, cnNames));
                }

                // Create a disconnect operation for each runspace to disconnect.
                foreach (PSSession psSession in psSessions.Values)
                {
                    if (ShouldProcess(psSession.Name, VerbsCommunications.Disconnect))
                    {
                        // PS session disconnection is not supported for VM/Container sessions.
                        if (psSession.ComputerType != TargetMachineType.RemoteMachine)
                        {
                            // Write error record.
                            string msg = StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnectedForVMContainerSession,
                                                           psSession.Name, psSession.ComputerName, psSession.ComputerType);
                            Exception   reason      = new PSNotSupportedException(msg);
                            ErrorRecord errorRecord = new ErrorRecord(reason, "CannotDisconnectVMContainerSession", ErrorCategory.InvalidOperation, psSession);
                            WriteError(errorRecord);
                            continue;
                        }

                        // Can only disconnect an Opened runspace.
                        if (psSession.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                        {
                            // Update the connectionInfo object with passed in session options.
                            if (_sessionOption != null)
                            {
                                psSession.Runspace.ConnectionInfo.SetSessionOptions(_sessionOption);
                            }

                            // Validate the ConnectionInfo IdleTimeout value against the MaxIdleTimeout
                            // value returned by the server and the hard coded minimum allowed value.
                            if (!ValidateIdleTimeout(psSession))
                            {
                                continue;
                            }

                            DisconnectRunspaceOperation disconnectOperation = new DisconnectRunspaceOperation(psSession, _stream);
                            disconnectOperations.Add(disconnectOperation);
                        }
                        else if (psSession.Runspace.RunspaceStateInfo.State != RunspaceState.Disconnected)
                        {
                            // Write error record.
                            string      msg         = StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnected, psSession.Name);
                            Exception   reason      = new RuntimeException(msg);
                            ErrorRecord errorRecord = new ErrorRecord(reason, "CannotDisconnectSessionWhenNotOpened", ErrorCategory.InvalidOperation, psSession);
                            WriteError(errorRecord);
                        }
                        else
                        {
                            // Session is already disconnected.  Write to output.
                            WriteObject(psSession);
                        }
                    }
                }
            }
            catch (PSRemotingDataStructureException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (PSRemotingTransportException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (RemoteException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (InvalidRunspaceStateException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }

            if (disconnectOperations.Count > 0)
            {
                // Make sure operations are not set as complete while processing input.
                _operationsComplete.Reset();

                // Submit list of disconnect operations.
                _throttleManager.SubmitOperations(disconnectOperations);

                // Write any output now.
                Collection <object> streamObjects = _stream.ObjectReader.NonBlockingRead();
                foreach (object streamObject in streamObjects)
                {
                    WriteStreamObject((Action <Cmdlet>)streamObject);
                }
            }
        }
Ejemplo n.º 2
0
 protected override void ProcessRecord()
 {
     List<IThrottleOperation> operations = new List<IThrottleOperation>();
     try
     {
         Dictionary<Guid, PSSession> matchingRunspaces;
         if (base.ParameterSetName == "Session")
         {
             if ((this.remotePSSessionInfo == null) || (this.remotePSSessionInfo.Length == 0))
             {
                 return;
             }
             matchingRunspaces = new Dictionary<Guid, PSSession>();
             foreach (PSSession session in this.remotePSSessionInfo)
             {
                 matchingRunspaces.Add(session.InstanceId, session);
             }
         }
         else
         {
             matchingRunspaces = base.GetMatchingRunspaces(false, true);
         }
         string localhostWithNetworkAccessEnabled = this.GetLocalhostWithNetworkAccessEnabled(matchingRunspaces);
         if (!string.IsNullOrEmpty(localhostWithNetworkAccessEnabled))
         {
             base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.EnableNetworkAccessWarning, localhostWithNetworkAccessEnabled));
         }
         foreach (PSSession session2 in matchingRunspaces.Values)
         {
             if (base.ShouldProcess(session2.Name, "Disconnect"))
             {
                 if (session2.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                 {
                     if (this.sessionOption != null)
                     {
                         session2.Runspace.ConnectionInfo.SetSessionOptions(this.sessionOption);
                     }
                     if (this.ValidateIdleTimeout(session2))
                     {
                         DisconnectRunspaceOperation item = new DisconnectRunspaceOperation(session2, this.stream);
                         operations.Add(item);
                     }
                 }
                 else if (session2.Runspace.RunspaceStateInfo.State != RunspaceState.Disconnected)
                 {
                     Exception exception = new RuntimeException(StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnected, session2.Name));
                     ErrorRecord errorRecord = new ErrorRecord(exception, "CannotDisconnectSessionWhenNotOpened", ErrorCategory.InvalidOperation, session2);
                     base.WriteError(errorRecord);
                 }
                 else
                 {
                     base.WriteObject(session2);
                 }
             }
         }
     }
     catch (PSRemotingDataStructureException)
     {
         this.operationsComplete.Set();
         throw;
     }
     catch (PSRemotingTransportException)
     {
         this.operationsComplete.Set();
         throw;
     }
     catch (RemoteException)
     {
         this.operationsComplete.Set();
         throw;
     }
     catch (InvalidRunspaceStateException)
     {
         this.operationsComplete.Set();
         throw;
     }
     if (operations.Count > 0)
     {
         this.operationsComplete.Reset();
         this.throttleManager.SubmitOperations(operations);
         foreach (object obj2 in this.stream.ObjectReader.NonBlockingRead())
         {
             base.WriteStreamObject((Action<Cmdlet>) obj2);
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Perform runspace disconnect processing on all input.
        /// </summary>
        protected override void ProcessRecord()
        {
            Dictionary<Guid, PSSession> psSessions;
            List<IThrottleOperation> disconnectOperations = new List<IThrottleOperation>();

            try
            {
                // Get all remote runspaces to disconnect.
                if (ParameterSetName == DisconnectPSSessionCommand.SessionParameterSet)
                {
                    if (Session == null || Session.Length == 0)
                    {
                        return;
                    }

                    psSessions = new Dictionary<Guid, PSSession>();
                    foreach (PSSession psSession in Session)
                    {
                        psSessions.Add(psSession.InstanceId, psSession);
                    }
                }
                else
                {
                    psSessions = GetMatchingRunspaces(false, true);
                }

                // Look for local sessions that have the EnableNetworkAccess property set and 
                // return a string containing all of the session names.  Emit a warning for 
                // these sessions.
                string cnNames = GetLocalhostWithNetworkAccessEnabled(psSessions);
                if (!string.IsNullOrEmpty(cnNames))
                {
                    WriteWarning(
                        StringUtil.Format(RemotingErrorIdStrings.EnableNetworkAccessWarning, cnNames));
                }

                // Create a disconnect operation for each runspace to disconnect.
                foreach (PSSession psSession in psSessions.Values)
                {
                    if (ShouldProcess(psSession.Name, VerbsCommunications.Disconnect))
                    {
                        // PS session disconnection is not supported for VM/Container sessions.
                        if (psSession.ComputerType != TargetMachineType.RemoteMachine)
                        {
                            // Write error record.
                            string msg = StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnectedForVMContainerSession,
                                psSession.Name, psSession.ComputerName, psSession.ComputerType);
                            Exception reason = new PSNotSupportedException(msg);
                            ErrorRecord errorRecord = new ErrorRecord(reason, "CannotDisconnectVMContainerSession", ErrorCategory.InvalidOperation, psSession);
                            WriteError(errorRecord);
                            continue;
                        }

                        // Can only disconnect an Opened runspace.
                        if (psSession.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                        {
                            // Update the connectionInfo object with passed in session options.
                            if (_sessionOption != null)
                            {
                                psSession.Runspace.ConnectionInfo.SetSessionOptions(_sessionOption);
                            }

                            // Validate the ConnectionInfo IdleTimeout value against the MaxIdleTimeout
                            // value returned by the server and the hard coded minimum allowed value.
                            if (!ValidateIdleTimeout(psSession))
                            {
                                continue;
                            }

                            DisconnectRunspaceOperation disconnectOperation = new DisconnectRunspaceOperation(psSession, _stream);
                            disconnectOperations.Add(disconnectOperation);
                        }
                        else if (psSession.Runspace.RunspaceStateInfo.State != RunspaceState.Disconnected)
                        {
                            // Write error record.
                            string msg = StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnected, psSession.Name);
                            Exception reason = new RuntimeException(msg);
                            ErrorRecord errorRecord = new ErrorRecord(reason, "CannotDisconnectSessionWhenNotOpened", ErrorCategory.InvalidOperation, psSession);
                            WriteError(errorRecord);
                        }
                        else
                        {
                            // Session is already disconnected.  Write to output.
                            WriteObject(psSession);
                        }
                    }
                }
            }
            catch (PSRemotingDataStructureException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (PSRemotingTransportException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (RemoteException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }
            catch (InvalidRunspaceStateException)
            {
                // Allow cmdlet to end and then re-throw exception.
                _operationsComplete.Set();
                throw;
            }

            if (disconnectOperations.Count > 0)
            {
                // Make sure operations are not set as complete while processing input.
                _operationsComplete.Reset();

                // Submit list of disconnect operations.
                _throttleManager.SubmitOperations(disconnectOperations);

                // Write any output now.
                Collection<object> streamObjects = _stream.ObjectReader.NonBlockingRead();
                foreach (object streamObject in streamObjects)
                {
                    WriteStreamObject((Action<Cmdlet>)streamObject);
                }
            }
        }
        protected override void ProcessRecord()
        {
            List <IThrottleOperation> operations = new List <IThrottleOperation>();

            try
            {
                Dictionary <Guid, PSSession> matchingRunspaces;
                if (base.ParameterSetName == "Session")
                {
                    if ((this.remotePSSessionInfo == null) || (this.remotePSSessionInfo.Length == 0))
                    {
                        return;
                    }
                    matchingRunspaces = new Dictionary <Guid, PSSession>();
                    foreach (PSSession session in this.remotePSSessionInfo)
                    {
                        matchingRunspaces.Add(session.InstanceId, session);
                    }
                }
                else
                {
                    matchingRunspaces = base.GetMatchingRunspaces(false, true);
                }
                string localhostWithNetworkAccessEnabled = this.GetLocalhostWithNetworkAccessEnabled(matchingRunspaces);
                if (!string.IsNullOrEmpty(localhostWithNetworkAccessEnabled))
                {
                    base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.EnableNetworkAccessWarning, localhostWithNetworkAccessEnabled));
                }
                foreach (PSSession session2 in matchingRunspaces.Values)
                {
                    if (base.ShouldProcess(session2.Name, "Disconnect"))
                    {
                        if (session2.Runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                        {
                            if (this.sessionOption != null)
                            {
                                session2.Runspace.ConnectionInfo.SetSessionOptions(this.sessionOption);
                            }
                            if (this.ValidateIdleTimeout(session2))
                            {
                                DisconnectRunspaceOperation item = new DisconnectRunspaceOperation(session2, this.stream);
                                operations.Add(item);
                            }
                        }
                        else if (session2.Runspace.RunspaceStateInfo.State != RunspaceState.Disconnected)
                        {
                            Exception   exception   = new RuntimeException(StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeDisconnected, session2.Name));
                            ErrorRecord errorRecord = new ErrorRecord(exception, "CannotDisconnectSessionWhenNotOpened", ErrorCategory.InvalidOperation, session2);
                            base.WriteError(errorRecord);
                        }
                        else
                        {
                            base.WriteObject(session2);
                        }
                    }
                }
            }
            catch (PSRemotingDataStructureException)
            {
                this.operationsComplete.Set();
                throw;
            }
            catch (PSRemotingTransportException)
            {
                this.operationsComplete.Set();
                throw;
            }
            catch (RemoteException)
            {
                this.operationsComplete.Set();
                throw;
            }
            catch (InvalidRunspaceStateException)
            {
                this.operationsComplete.Set();
                throw;
            }
            if (operations.Count > 0)
            {
                this.operationsComplete.Reset();
                this.throttleManager.SubmitOperations(operations);
                foreach (object obj2 in this.stream.ObjectReader.NonBlockingRead())
                {
                    base.WriteStreamObject((Action <Cmdlet>)obj2);
                }
            }
        }