コード例 #1
0
        protected override void ProcessRecord()
        {
            IHostSupportsInteractiveSession session = base.Host as IHostSupportsInteractiveSession;

            if (session == null)
            {
                base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.HostDoesNotSupportPushRunspace)), PSRemotingErrorId.HostDoesNotSupportPushRunspace.ToString(), ErrorCategory.InvalidArgument, null));
            }

            /*
             * else if (((base.Context != null) && (base.Context.EngineHostInterface != null)) && ((base.Context.EngineHostInterface.ExternalHost != null) && (base.Context.EngineHostInterface.ExternalHost is ServerRemoteHost)))
             * {
             * base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.RemoteHostDoesNotSupportPushRunspace)), PSRemotingErrorId.RemoteHostDoesNotSupportPushRunspace.ToString(), ErrorCategory.InvalidArgument, null));
             * }
             */
            else
            {
                InternalHost targetObject = base.Host as InternalHost;
                if ((targetObject != null) && targetObject.HostInNestedPrompt())
                {
                    base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.HostInNestedPrompt, new object[0])), "HostInNestedPrompt", ErrorCategory.InvalidOperation, targetObject));
                }
                RemoteRunspace runspaceMatchingRunspaceId = null;
                string         parameterSetName           = base.ParameterSetName;
                if (parameterSetName != null)
                {
                    if (!(parameterSetName == "ComputerName"))
                    {
                        if (parameterSetName == "Uri")
                        {
                            runspaceMatchingRunspaceId = this.CreateRunspaceWhenUriParameterSpecified();
                        }
                        else if (parameterSetName == "Session")
                        {
                            runspaceMatchingRunspaceId = (RemoteRunspace)this.remoteRunspaceInfo.Runspace;
                        }
                        else if (parameterSetName == "InstanceId")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingRunspaceId(this.InstanceId);
                        }
                        else if (parameterSetName == "Id")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingSessionId(this.Id);
                        }
                        else if (parameterSetName == "Name")
                        {
                            runspaceMatchingRunspaceId = this.GetRunspaceMatchingName(this.Name);
                        }
                    }
                    else
                    {
                        runspaceMatchingRunspaceId = this.CreateRunspaceWhenComputerNameParameterSpecified();
                    }
                }
                if (runspaceMatchingRunspaceId != null)
                {
                    if (runspaceMatchingRunspaceId.RunspaceStateInfo.State == RunspaceState.Disconnected)
                    {
                        if (!runspaceMatchingRunspaceId.CanConnect)
                        {
                            string message = StringUtil.Format(RemotingErrorIdStrings.SessionNotAvailableForConnection, new object[0]);
                            base.WriteError(new ErrorRecord(new RuntimeException(message), "EnterPSSessionCannotConnectDisconnectedSession", ErrorCategory.InvalidOperation, runspaceMatchingRunspaceId));
                            return;
                        }
                        Exception innerException = null;
                        try
                        {
                            runspaceMatchingRunspaceId.Connect();
                        }
                        catch (PSRemotingTransportException exception2)
                        {
                            innerException = exception2;
                        }
                        catch (PSInvalidOperationException exception3)
                        {
                            innerException = exception3;
                        }
                        catch (InvalidRunspacePoolStateException exception4)
                        {
                            innerException = exception4;
                        }
                        if (innerException != null)
                        {
                            string str2 = StringUtil.Format(RemotingErrorIdStrings.SessionConnectFailed, new object[0]);
                            base.WriteError(new ErrorRecord(new RuntimeException(str2, innerException), "EnterPSSessionConnectSessionFailed", ErrorCategory.InvalidOperation, runspaceMatchingRunspaceId));
                            return;
                        }
                        if (runspaceMatchingRunspaceId.RunspaceAvailability == RunspaceAvailability.Busy)
                        {
                            string str3 = StringUtil.Format(RemotingErrorIdStrings.EnterPSSessionDisconnected, runspaceMatchingRunspaceId.Name);
                            base.WriteError(new ErrorRecord(new RuntimeException(str3, innerException), "EnterPSSessionConnectSessionNotAvailable", ErrorCategory.InvalidOperation, this.remoteRunspaceInfo));
                            runspaceMatchingRunspaceId.DisconnectAsync();
                            return;
                        }
                    }
                    if (runspaceMatchingRunspaceId.RunspaceStateInfo.State != RunspaceState.Opened)
                    {
                        if (base.ParameterSetName == "Session")
                        {
                            string str4 = (this.remoteRunspaceInfo != null) ? this.remoteRunspaceInfo.Name : string.Empty;
                            base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.EnterPSSessionBrokenSession, new object[] { str4, runspaceMatchingRunspaceId.ConnectionInfo.ComputerName, runspaceMatchingRunspaceId.InstanceId })), PSRemotingErrorId.PushedRunspaceMustBeOpen.ToString(), ErrorCategory.InvalidArgument, null));
                        }
                        else
                        {
                            base.WriteError(new ErrorRecord(new ArgumentException(base.GetMessage(RemotingErrorIdStrings.PushedRunspaceMustBeOpen)), PSRemotingErrorId.PushedRunspaceMustBeOpen.ToString(), ErrorCategory.InvalidArgument, null));
                        }
                    }
                    else
                    {
                        if (runspaceMatchingRunspaceId.RunspaceAvailability != RunspaceAvailability.Available)
                        {
                            base.WriteWarning(base.GetMessage(RunspaceStrings.RunspaceNotReady));
                        }
                        try
                        {
                            session.PushRunspace(runspaceMatchingRunspaceId);
                        }
                        catch (Exception)
                        {
                            if ((runspaceMatchingRunspaceId != null) && runspaceMatchingRunspaceId.ShouldCloseOnPop)
                            {
                                runspaceMatchingRunspaceId.Close();
                            }
                            throw;
                        }
                    }
                }
            }
        }