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); }
private void AddConnectionRetryHandler(PSInvokeExpressionSyncJob job) { if (job != null) { foreach (PowerShell shell in job.GetPowerShells()) { if (shell.RemotePowerShell != null) { shell.RemotePowerShell.RCConnectionNotification += new EventHandler <PSConnectionRetryStatusEventArgs>(this.RCConnectionNotificationHandler); } } } }
private void CreateAndRunSyncJob() { lock (this.jobSyncObject) { if (!this.nojob) { this.throttleManager.ThrottleLimit = this.ThrottleLimit; this.throttleManager.ThrottleComplete += new EventHandler <EventArgs>(this.HandleThrottleComplete); this.operationsComplete.Reset(); this.disconnectComplete = new ManualResetEvent(false); this.job = new PSInvokeExpressionSyncJob(base.Operations, this.throttleManager); this.job.HideComputerName = this.hideComputerName; this.job.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleJobStateChanged); this.AddConnectionRetryHandler(this.job); this.job.StartOperations(base.Operations); } } }