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); }
private string GetRunspaceName(int rsIndex, out int rsId) { string str = PSSession.GenerateRunspaceName(out rsId); if ((this.names != null) && (rsIndex < this.names.Length)) { str = this.names[rsIndex]; } return(str); }
private List <PSSession> GetDisconnectedSessions(PSInvokeExpressionSyncJob job) { List <PSSession> list = new List <PSSession>(); foreach (PowerShell shell in job.GetPowerShells()) { string cmdStr = ((shell.Commands != null) && (shell.Commands.Commands.Count > 0)) ? shell.Commands.Commands[0].CommandText : string.Empty; ConnectCommandInfo info = new ConnectCommandInfo(shell.InstanceId, cmdStr); RunspacePool runspacePool = null; if (shell.RunspacePool != null) { runspacePool = shell.RunspacePool; } else { object runspaceConnection = shell.GetRunspaceConnection(); RunspacePool pool2 = runspaceConnection as RunspacePool; if (pool2 != null) { runspacePool = pool2; } else { RemoteRunspace runspace = runspaceConnection as RemoteRunspace; if (runspace != null) { runspacePool = runspace.RunspacePool; } } } if (runspacePool != null) { if (runspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Disconnected) { if (!base.InvokeAndDisconnect || (runspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Opened)) { continue; } runspacePool.Disconnect(); } string name = runspacePool.RemoteRunspacePoolInternal.Name; if (string.IsNullOrEmpty(name)) { int num; name = PSSession.GenerateRunspaceName(out num); } RunspacePool pool3 = new RunspacePool(true, runspacePool.RemoteRunspacePoolInternal.InstanceId, name, new ConnectCommandInfo[] { info }, runspacePool.RemoteRunspacePoolInternal.ConnectionInfo, base.Host, base.Context.TypeTable); RemoteRunspace remoteRunspace = new RemoteRunspace(pool3); list.Add(new PSSession(remoteRunspace)); } } return(list); }