private void WriteConnectedPSSession() { // Use temporary variable because we need to preserve _session class variable // for later clean up. PSSession outSession = _session; if (_queryRunspaces != null) { lock (s_LockObject) { // Pass back the original session if possible. if (_oldSession != null && _oldSession.InsertRunspace(_session.Runspace as RemoteRunspace)) { outSession = _oldSession; _retryList.Add(_oldSession); } else { _retryList.Add(_session); } } } if (_writeStream.ObjectWriter.IsOpen) { // This code is based on ThrottleManager infrastructure // and this particular method may be called on a thread that // is different from Pipeline Execution Thread. Hence using // a delegate to perform the WriteObject. Action <Cmdlet> outputWriter = (Cmdlet cmdlet) => cmdlet.WriteObject(outSession); _writeStream.ObjectWriter.Write(outputWriter); } }
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); }
private void QueryForAndConnectCommands(string name, Guid instanceId) { Runspace[] runspaceArray; WSManConnectionInfo connectionObject = this.GetConnectionObject(); try { runspaceArray = Runspace.GetRunspaces(connectionObject, base.Host, QueryRunspaces.BuiltInTypesTable); } catch (RuntimeException exception) { int num; string message = StringUtil.Format(RemotingErrorIdStrings.QueryForRunspacesFailed, connectionObject.ComputerName, QueryRunspaces.ExtractMessage(exception.InnerException, out num)); string fQEIDFromTransportError = WSManTransportManagerUtils.GetFQEIDFromTransportError(num, "ReceivePSSessionQueryForSessionFailed"); Exception exception2 = new RuntimeException(message, exception.InnerException); ErrorRecord errorRecord = new ErrorRecord(exception2, fQEIDFromTransportError, ErrorCategory.InvalidOperation, connectionObject); base.WriteError(errorRecord); return; } string str3 = null; if (!string.IsNullOrEmpty(this.ConfigurationName)) { str3 = (this.ConfigurationName.IndexOf("http://schemas.microsoft.com/powershell/", StringComparison.OrdinalIgnoreCase) != -1) ? this.ConfigurationName : ("http://schemas.microsoft.com/powershell/" + this.ConfigurationName); } foreach (Runspace runspace in runspaceArray) { if (this._stopProcessing) { return; } if (str3 != null) { WSManConnectionInfo connectionInfo = runspace.ConnectionInfo as WSManConnectionInfo; if ((connectionInfo != null) && !str3.Equals(connectionInfo.ShellUri, StringComparison.OrdinalIgnoreCase)) { continue; } } bool flag = false; if (!string.IsNullOrEmpty(name) && (string.Compare(name, ((RemoteRunspace)runspace).RunspacePool.RemoteRunspacePoolInternal.Name, StringComparison.OrdinalIgnoreCase) == 0)) { flag = true; } else if (instanceId.Equals(runspace.InstanceId)) { flag = true; } if (flag && base.ShouldProcess(((RemoteRunspace)runspace).Name, "Receive")) { Exception exception3; PSSession item = base.RunspaceRepository.GetItem(runspace.InstanceId); PSSession session2 = this.ConnectSession(item, out exception3); if (session2 != null) { base.RunspaceRepository.AddOrReplace(session2); PSRemotingJob job = this.FindJobForSession(session2); if (this.OutTarget == Microsoft.PowerShell.Commands.OutTarget.Host) { this.ConnectSessionToHost(session2, job); return; } this.ConnectSessionToJob(session2, job); return; } PSSession session = new PSSession(runspace as RemoteRunspace); session2 = this.ConnectSession(session, out exception3); if (session2 != null) { if (item != null) { session2 = item.InsertRunspace(session2.Runspace as RemoteRunspace) ? item : session2; } base.RunspaceRepository.AddOrReplace(session2); if (this.OutTarget == Microsoft.PowerShell.Commands.OutTarget.Job) { this.ConnectSessionToJob(session2, null); return; } this.ConnectSessionToHost(session2, null); return; } string str4 = StringUtil.Format(RemotingErrorIdStrings.RunspaceCannotBeConnected, session.Name); base.WriteError(new ErrorRecord(new ArgumentException(str4, exception3), "ReceivePSSessionCannotConnectSession", ErrorCategory.InvalidOperation, session)); return; } } }