protected void UpdateRunspaceAvailability(RunspaceState runspaceState, bool raiseEvent) { System.Management.Automation.Runspaces.RunspaceAvailability runspaceAvailability = this.RunspaceAvailability; RemoteRunspace runspace = this as RemoteRunspace; ConnectCommandInfo info = (runspace != null) ? runspace.RemoteCommand : null; switch (runspaceAvailability) { case System.Management.Automation.Runspaces.RunspaceAvailability.None: if (runspaceState == RunspaceState.Opened) { this.RunspaceAvailability = ((info == null) && (this.GetCurrentlyRunningPipeline() == null)) ? System.Management.Automation.Runspaces.RunspaceAvailability.Available : System.Management.Automation.Runspaces.RunspaceAvailability.Busy; } break; case System.Management.Automation.Runspaces.RunspaceAvailability.Available: case System.Management.Automation.Runspaces.RunspaceAvailability.AvailableForNestedCommand: case System.Management.Automation.Runspaces.RunspaceAvailability.Busy: switch (runspaceState) { case RunspaceState.Closed: case RunspaceState.Closing: case RunspaceState.Broken: case RunspaceState.Disconnected: this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.None; goto Label_007D; } break; } Label_007D: if (raiseEvent && (this.RunspaceAvailability != runspaceAvailability)) { this.OnAvailabilityChanged(new RunspaceAvailabilityEventArgs(this.RunspaceAvailability)); } }
internal RemotePipeline(RemoteRunspace runspace) : this(runspace, false, false) { if (runspace.RemoteCommand == null) { throw new InvalidOperationException(PipelineStrings.InvalidRemoteCommand); } this._connectCmdInfo = runspace.RemoteCommand; this._commands.Add(this._connectCmdInfo.Command); this.SetPipelineState(PipelineState.Disconnected, null); this._powershell = new System.Management.Automation.PowerShell(this._connectCmdInfo, this._inputStream, this._outputStream, this._errorStream, ((RemoteRunspace)this._runspace).RunspacePool); this._powershell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(this.HandleInvocationStateChanged); }
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); }
/// <summary> /// Constructs a remote pipeline object associated with a remote running /// command but in a disconnected state. /// </summary> /// <param name="runspace">Remote runspace associated with running command.</param> internal RemotePipeline(RemoteRunspace runspace) : this(runspace, false, false) { if (runspace.RemoteCommand == null) { throw new InvalidOperationException(PipelineStrings.InvalidRemoteCommand); } _connectCmdInfo = runspace.RemoteCommand; _commands.Add(_connectCmdInfo.Command); // Beginning state will be disconnected. SetPipelineState(PipelineState.Disconnected, null); // Create the underlying powershell object. _powershell = new PowerShell(_connectCmdInfo, _inputStream, _outputStream, _errorStream, ((RemoteRunspace)_runspace).RunspacePool); _powershell.InvocationStateChanged += HandleInvocationStateChanged; }
internal void UpdateRunspaceAvailability(PipelineState pipelineState, bool raiseEvent) { System.Management.Automation.Runspaces.RunspaceAvailability runspaceAvailability = this.RunspaceAvailability; switch (runspaceAvailability) { case System.Management.Automation.Runspaces.RunspaceAvailability.None: if (pipelineState == PipelineState.Running) { this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Busy; } break; case System.Management.Automation.Runspaces.RunspaceAvailability.Available: switch (pipelineState) { case PipelineState.Running: this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Busy; goto Label_0181; case PipelineState.Disconnected: this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.None; goto Label_0181; } break; case System.Management.Automation.Runspaces.RunspaceAvailability.AvailableForNestedCommand: switch (pipelineState) { case PipelineState.Running: this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Busy; goto Label_0181; case PipelineState.Completed: this.RunspaceAvailability = this.InNestedPrompt ? System.Management.Automation.Runspaces.RunspaceAvailability.AvailableForNestedCommand : System.Management.Automation.Runspaces.RunspaceAvailability.Available; goto Label_0181; } break; case System.Management.Automation.Runspaces.RunspaceAvailability.Busy: switch (pipelineState) { case PipelineState.Stopped: case PipelineState.Completed: case PipelineState.Failed: if (this.InNestedPrompt || (!(this is RemoteRunspace) && this.Debugger.InBreakpoint)) { this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.AvailableForNestedCommand; } else { RemoteRunspace runspace = this as RemoteRunspace; ConnectCommandInfo info = (runspace != null) ? runspace.RemoteCommand : null; if (((pipelineState == PipelineState.Completed) || (pipelineState == PipelineState.Failed)) && (info != null)) { runspace.RunspacePool.RemoteRunspacePoolInternal.ConnectCommands = null; info = null; } Pipeline currentlyRunningPipeline = this.GetCurrentlyRunningPipeline(); if (currentlyRunningPipeline == null) { if (info == null) { if (runspace != null) { this.RunspaceAvailability = runspace.RunspacePool.RemoteRunspacePoolInternal.AvailableForConnection ? System.Management.Automation.Runspaces.RunspaceAvailability.Available : System.Management.Automation.Runspaces.RunspaceAvailability.Busy; } else { this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Available; } } } else if (currentlyRunningPipeline.PipelineStateInfo.State == PipelineState.Running) { this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Busy; } else { this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.Available; } } goto Label_0181; case PipelineState.Disconnected: this.RunspaceAvailability = System.Management.Automation.Runspaces.RunspaceAvailability.None; goto Label_0181; } break; } Label_0181: if (raiseEvent && (this.RunspaceAvailability != runspaceAvailability)) { this.OnAvailabilityChanged(new RunspaceAvailabilityEventArgs(this.RunspaceAvailability)); } }