Example #1
0
 private PSSession TryGetSessionFromServer(PSSession session)
 {
     if (session.Runspace is RemoteRunspace)
     {
         RemoteRunspace remoteRunspace = null;
         foreach (Runspace runspace2 in Runspace.GetRunspaces(session.Runspace.ConnectionInfo, base.Host, QueryRunspaces.BuiltInTypesTable))
         {
             if (runspace2.InstanceId == session.Runspace.InstanceId)
             {
                 remoteRunspace = runspace2 as RemoteRunspace;
                 break;
             }
         }
         if (remoteRunspace != null)
         {
             session = session.InsertRunspace(remoteRunspace) ? session : new PSSession(remoteRunspace);
             return session;
         }
     }
     return null;
 }
Example #2
0
        /// <summary>
        /// Helper method to attempt to retrieve a disconnected runspace object
        /// from the server, based on the provided session object.
        /// </summary>
        /// <param name="session">PSSession</param>
        /// <returns>PSSession</returns>
        private PSSession TryGetSessionFromServer(PSSession session)
        {
            RemoteRunspace remoteRunspace = session.Runspace as RemoteRunspace;
            if (remoteRunspace == null)
            {
                return null;
            }

            remoteRunspace = null;
            Runspace[] runspaces = Runspace.GetRunspaces(session.Runspace.ConnectionInfo, this.Host, QueryRunspaces.BuiltInTypesTable);
            foreach (Runspace runspace in runspaces)
            {
                if (runspace.InstanceId == session.Runspace.InstanceId)
                {
                    remoteRunspace = runspace as RemoteRunspace;
                    break;
                }
            }

            if (remoteRunspace != null)
            {
                // Try inserting connected runspace into existing PSSession.
                session = session.InsertRunspace(remoteRunspace) ? session : new PSSession(remoteRunspace);
                return session;
            }

            return null;
        }