コード例 #1
0
        private RemoteRunspace CreateTemporaryRemoteRunspace(PSHost host, WSManConnectionInfo connectionInfo)
        {
            int            num;
            string         name     = PSSession.GenerateRunspaceName(out num);
            RemoteRunspace runspace = new RemoteRunspace(Utils.GetTypeTableFromExecutionContextTLS(), connectionInfo, host, this.SessionOption.ApplicationArguments, name, num);

            runspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
            this.stream = new ObjectStream();
            try
            {
                runspace.Open();
                runspace.ShouldCloseOnPop = true;
            }
            finally
            {
                runspace.URIRedirectionReported -= new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
                this.stream.ObjectWriter.Close();
                if (runspace.RunspaceStateInfo.State != RunspaceState.Opened)
                {
                    runspace.Dispose();
                    runspace = null;
                }
            }
            return(runspace);
        }
コード例 #2
0
        protected override void ProcessRecord()
        {
            ICollection <PSSession> remoteRunspaceInfos = null;
            string parameterSetName = base.ParameterSetName;

            if (parameterSetName != null)
            {
                if ((!(parameterSetName == "ComputerName") && !(parameterSetName == "Name")) && (!(parameterSetName == "InstanceId") && !(parameterSetName == "Id")))
                {
                    if (parameterSetName == "Session")
                    {
                        remoteRunspaceInfos = this.remoteRunspaceInfos;
                        goto Label_0076;
                    }
                }
                else
                {
                    remoteRunspaceInfos = base.GetMatchingRunspaces(false, true).Values;
                    goto Label_0076;
                }
            }
            remoteRunspaceInfos = new Collection <PSSession>();
Label_0076:
            foreach (PSSession session in remoteRunspaceInfos)
            {
                RemoteRunspace targetObject = (RemoteRunspace)session.Runspace;
                if (base.ShouldProcess(targetObject.ConnectionInfo.ComputerName, "Remove"))
                {
                    if (session.Runspace.RunspaceStateInfo.State == RunspaceState.Disconnected)
                    {
                        bool flag;
                        try
                        {
                            session.Runspace.Connect();
                            flag = true;
                        }
                        catch (InvalidRunspaceStateException)
                        {
                            flag = false;
                        }
                        catch (PSRemotingTransportException)
                        {
                            flag = false;
                        }
                        if (!flag)
                        {
                            Exception   exception   = new RuntimeException(StringUtil.Format(RemotingErrorIdStrings.RemoveRunspaceNotConnected, targetObject.Name));
                            ErrorRecord errorRecord = new ErrorRecord(exception, "RemoveSessionCannotConnectToServer", ErrorCategory.InvalidOperation, targetObject);
                            base.WriteError(errorRecord);
                        }
                    }
                    try
                    {
                        targetObject.Dispose();
                    }
                    catch (PSRemotingTransportException)
                    {
                    }
                    try
                    {
                        base.RunspaceRepository.Remove(session);
                    }
                    catch (ArgumentException)
                    {
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Do the following actions:
        ///     1. If runspace is in opened state,
        ///             a. stop any execution in process in the runspace
        ///             b. close the runspace
        ///     2. Remove the runspace from the global cache.
        /// </summary>
        protected override void ProcessRecord()
        {
            ICollection <PSSession> toRemove = null;

            switch (ParameterSetName)
            {
            case RemovePSSessionCommand.ComputerNameParameterSet:
            case RemovePSSessionCommand.NameParameterSet:
            case RemovePSSessionCommand.InstanceIdParameterSet:
            case RemovePSSessionCommand.IdParameterSet:
            case RemovePSSessionCommand.ContainerIdParameterSet:
            case RemovePSSessionCommand.VMIdParameterSet:
            case RemovePSSessionCommand.VMNameParameterSet:
            {
                Dictionary <Guid, PSSession> matches = GetMatchingRunspaces(false, true);

                toRemove = matches.Values;
            }

            break;

            case RemovePSSessionCommand.SessionParameterSet:
            {
                toRemove = Session;
            }

            break;

            default:
                Diagnostics.Assert(false, "Invalid Parameter Set");
                toRemove = new Collection <PSSession>();    // initialize toRemove to turn off PREfast warning about it being null
                break;
            }

            foreach (PSSession remoteRunspaceInfo in toRemove)
            {
                RemoteRunspace remoteRunspace = (RemoteRunspace)remoteRunspaceInfo.Runspace;

                if (ShouldProcess(remoteRunspace.ConnectionInfo.ComputerName, "Remove"))
                {
                    // If the remote runspace is in a disconnected state, first try to connect it so that
                    // it can be removed from both the client and server.
                    if (remoteRunspaceInfo.Runspace.RunspaceStateInfo.State == RunspaceState.Disconnected)
                    {
                        bool ConnectSucceeded;

                        try
                        {
                            remoteRunspaceInfo.Runspace.Connect();
                            ConnectSucceeded = true;
                        }
                        catch (InvalidRunspaceStateException)
                        {
                            ConnectSucceeded = false;
                        }
                        catch (PSRemotingTransportException)
                        {
                            ConnectSucceeded = false;
                        }

                        if (!ConnectSucceeded)
                        {
                            // Write error notification letting user know that session cannot be removed
                            // from server due to lack of connection.
                            string msg = System.Management.Automation.Internal.StringUtil.Format(
                                RemotingErrorIdStrings.RemoveRunspaceNotConnected, remoteRunspace.PSSessionName);
                            Exception   reason      = new RuntimeException(msg);
                            ErrorRecord errorRecord = new ErrorRecord(reason, "RemoveSessionCannotConnectToServer",
                                                                      ErrorCategory.InvalidOperation, remoteRunspace);
                            WriteError(errorRecord);

                            // Continue removing the runspace from the client.
                        }
                    }

                    try
                    {
                        // Dispose internally calls Close() and Close()
                        // is a no-op if the state is not Opened, so just
                        // dispose the runspace
                        remoteRunspace.Dispose();
                    }
                    catch (PSRemotingTransportException)
                    {
                        // just ignore, there is some transport error
                        // on Close()
                    }

                    try
                    {
                        // Remove the runspace from the repository
                        this.RunspaceRepository.Remove(remoteRunspaceInfo);
                    }
                    catch (ArgumentException)
                    {
                        // just ignore, the runspace may already have
                        // been removed
                    }
                }
            }
        }
コード例 #4
0
 /// <inheritdoc />
 public void Dispose()
 {
     RemoteRunspace?.Dispose();
 }