internal ServerSteppablePipelineDriver(PowerShell powershell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse, ServerSteppablePipelineSubscriber eventSubscriber, PSDataCollection<object> powershellInput) { this.localPowerShell = powershell; this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.noInput = noInput; this.addToHistory = addToHistory; this.eventSubscriber = eventSubscriber; this.powershellInput = powershellInput; this.input = new PSDataCollection<object>(); this.inputEnumerator = this.input.GetEnumerator(); this.input.ReleaseOnEnumeration = true; this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, null); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); this.dsHandler.InputEndReceived += new EventHandler(this.HandleInputEndReceived); this.dsHandler.InputReceived += new EventHandler<RemoteDataEventArgs<object>>(this.HandleInputReceived); this.dsHandler.StopPowerShellReceived += new EventHandler(this.HandleStopReceived); this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived); this.dsHandler.OnSessionConnected += new EventHandler(this.HandleSessionConnected); if (rsToUse == null) { throw PSTraceSource.NewInvalidOperationException("RemotingErrorIdStrings", "NestedPipelineMissingRunspace", new object[0]); } this.localPowerShell.Runspace = rsToUse; eventSubscriber.SubscribeEvents(this); this.stateOfSteppablePipeline = PSInvocationState.NotStarted; }
/// <summary> /// Constructor for ServerRemoteHostUserInterface. /// </summary> internal ServerRemoteHostUserInterface(ServerRemoteHost remoteHost) { Dbg.Assert(remoteHost != null, "Expected remoteHost != null"); ServerRemoteHost = remoteHost; Dbg.Assert(!remoteHost.HostInfo.IsHostUINull, "Expected !remoteHost.HostInfo.IsHostUINull"); _serverMethodExecutor = remoteHost.ServerMethodExecutor; // Use HostInfo to duplicate host-RawUI as null or non-null based on the client's host-RawUI. RawUI = remoteHost.HostInfo.IsHostRawUINull ? null : new ServerRemoteHostRawUserInterface(this); }
internal ServerRemoteHost GetHostAssociatedWithPowerShell(HostInfo powerShellHostInfo, ServerRemoteHost runspaceServerRemoteHost) { HostInfo hostInfo; if (powerShellHostInfo.UseRunspaceHost) { hostInfo = runspaceServerRemoteHost.HostInfo; } else { hostInfo = powerShellHostInfo; } return new ServerRemoteHost(this.clientRunspacePoolId, this.clientPowerShellId, hostInfo, this.transportManager); }
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse) { this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.localPowerShell = powershell; this.extraPowerShell = extraPowerShell; this.localPowerShellOutput = new PSDataCollection<PSObject>(); this.noInput = noInput; this.addToHistory = addToHistory; this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, this.localPowerShell); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); if (!noInput) { this.input = new PSDataCollection<object>(); this.input.ReleaseOnEnumeration = true; this.input.IdleEvent += new EventHandler<EventArgs>(this.HandleIdleEvent); } this.RegisterPipelineOutputEventHandlers(this.localPowerShellOutput); if (this.localPowerShell != null) { this.RegisterPowerShellEventHandlers(this.localPowerShell); this.datasent[0] = false; } if (extraPowerShell != null) { this.RegisterPowerShellEventHandlers(extraPowerShell); this.datasent[1] = false; } this.RegisterDataStructureHandlerEventHandlers(this.dsHandler); if (rsToUse != null) { this.localPowerShell.Runspace = rsToUse; if (extraPowerShell != null) { extraPowerShell.Runspace = rsToUse; } } else { this.localPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell != null) { extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } } }
internal ServerRunspacePoolDriver(Guid clientRunspacePoolId, int minRunspaces, int maxRunspaces, PSThreadOptions threadOptions, ApartmentState apartmentState, HostInfo hostInfo, InitialSessionState initialSessionState, PSPrimitiveDictionary applicationPrivateData, ConfigurationDataFromXML configData, AbstractServerSessionTransportManager transportManager, bool isAdministrator, RemoteSessionCapability serverCapability, Hashtable configHash) { this.serverCapability = serverCapability; System.Management.Automation.Remoting.ServerRemoteHost host = new System.Management.Automation.Remoting.ServerRemoteHost(clientRunspacePoolId, Guid.Empty, hostInfo, transportManager); this.remoteHost = host; this.configData = configData; this.configHash = configHash; this.applicationPrivateData = applicationPrivateData; this.localRunspacePool = RunspaceFactory.CreateRunspacePool(minRunspaces, maxRunspaces, initialSessionState, host); PSThreadOptions options = configData.ShellThreadOptions.HasValue ? configData.ShellThreadOptions.Value : PSThreadOptions.UseCurrentThread; if ((threadOptions == PSThreadOptions.Default) || (threadOptions == options)) { this.localRunspacePool.ThreadOptions = options; } else { if (!isAdministrator) { throw new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.MustBeAdminToOverrideThreadOptions, new object[0])); } this.localRunspacePool.ThreadOptions = threadOptions; } ApartmentState state = configData.ShellThreadApartmentState.HasValue ? configData.ShellThreadApartmentState.Value : ApartmentState.Unknown; if ((apartmentState == ApartmentState.Unknown) || (apartmentState == state)) { this.localRunspacePool.ApartmentState = state; } else { this.localRunspacePool.ApartmentState = apartmentState; } this.clientRunspacePoolId = clientRunspacePoolId; this.dsHandler = new ServerRunspacePoolDataStructureHandler(this, transportManager); this.localRunspacePool.StateChanged += new EventHandler <RunspacePoolStateChangedEventArgs>(this.HandleRunspacePoolStateChanged); this.localRunspacePool.ForwardEvent += new EventHandler <PSEventArgs>(this.HandleRunspacePoolForwardEvent); this.localRunspacePool.RunspaceCreated += new EventHandler <RunspaceCreatedEventArgs>(this.HandleRunspaceCreated); this.localRunspacePool.RunspaceCreated += new EventHandler <RunspaceCreatedEventArgs>(this.HandleRunspaceCreatedForTypeTable); this.dsHandler.CreateAndInvokePowerShell += new EventHandler <RemoteDataEventArgs <RemoteDataObject <PSObject> > >(this.HandleCreateAndInvokePowerShell); this.dsHandler.GetCommandMetadata += new EventHandler <RemoteDataEventArgs <RemoteDataObject <PSObject> > >(this.HandleGetCommandMetadata); this.dsHandler.HostResponseReceived += new EventHandler <RemoteDataEventArgs <RemoteHostResponse> >(this.HandleHostResponseReceived); this.dsHandler.SetMaxRunspacesReceived += new EventHandler <RemoteDataEventArgs <PSObject> >(this.HandleSetMaxRunspacesReceived); this.dsHandler.SetMinRunspacesReceived += new EventHandler <RemoteDataEventArgs <PSObject> >(this.HandleSetMinRunspacesReceived); this.dsHandler.GetAvailableRunspacesReceived += new EventHandler <RemoteDataEventArgs <PSObject> >(this.HandleGetAvailalbeRunspacesReceived); }
internal ServerRunspacePoolDriver(Guid clientRunspacePoolId, int minRunspaces, int maxRunspaces, PSThreadOptions threadOptions, ApartmentState apartmentState, HostInfo hostInfo, InitialSessionState initialSessionState, PSPrimitiveDictionary applicationPrivateData, ConfigurationDataFromXML configData, AbstractServerSessionTransportManager transportManager, bool isAdministrator, RemoteSessionCapability serverCapability, Hashtable configHash) { this.serverCapability = serverCapability; System.Management.Automation.Remoting.ServerRemoteHost host = new System.Management.Automation.Remoting.ServerRemoteHost(clientRunspacePoolId, Guid.Empty, hostInfo, transportManager); this.remoteHost = host; this.configData = configData; this.configHash = configHash; this.applicationPrivateData = applicationPrivateData; this.localRunspacePool = RunspaceFactory.CreateRunspacePool(minRunspaces, maxRunspaces, initialSessionState, host); PSThreadOptions options = configData.ShellThreadOptions.HasValue ? configData.ShellThreadOptions.Value : PSThreadOptions.UseCurrentThread; if ((threadOptions == PSThreadOptions.Default) || (threadOptions == options)) { this.localRunspacePool.ThreadOptions = options; } else { if (!isAdministrator) { throw new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.MustBeAdminToOverrideThreadOptions, new object[0])); } this.localRunspacePool.ThreadOptions = threadOptions; } ApartmentState state = configData.ShellThreadApartmentState.HasValue ? configData.ShellThreadApartmentState.Value : ApartmentState.Unknown; if ((apartmentState == ApartmentState.Unknown) || (apartmentState == state)) { this.localRunspacePool.ApartmentState = state; } else { this.localRunspacePool.ApartmentState = apartmentState; } this.clientRunspacePoolId = clientRunspacePoolId; this.dsHandler = new ServerRunspacePoolDataStructureHandler(this, transportManager); this.localRunspacePool.StateChanged += new EventHandler<RunspacePoolStateChangedEventArgs>(this.HandleRunspacePoolStateChanged); this.localRunspacePool.ForwardEvent += new EventHandler<PSEventArgs>(this.HandleRunspacePoolForwardEvent); this.localRunspacePool.RunspaceCreated += new EventHandler<RunspaceCreatedEventArgs>(this.HandleRunspaceCreated); this.localRunspacePool.RunspaceCreated += new EventHandler<RunspaceCreatedEventArgs>(this.HandleRunspaceCreatedForTypeTable); this.dsHandler.CreateAndInvokePowerShell += new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(this.HandleCreateAndInvokePowerShell); this.dsHandler.GetCommandMetadata += new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(this.HandleGetCommandMetadata); this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived); this.dsHandler.SetMaxRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleSetMaxRunspacesReceived); this.dsHandler.SetMinRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleSetMinRunspacesReceived); this.dsHandler.GetAvailableRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleGetAvailalbeRunspacesReceived); }
internal void StartPowerShellCommand( PowerShell powershell, Guid powershellId, Guid runspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, #if !CORECLR // No ApartmentState In CoreCLR ApartmentState apartmentState, #endif ServerRemoteHost remoteHost, HostInfo hostInfo, RemoteStreamOptions streamOptions, bool addToHistory) { // For nested debugger command processing, invoke command on new local runspace since // the root script debugger runspace is unavailable (it is running a PS script or a // workflow function script). Runspace runspace = (remoteHost != null) ? RunspaceFactory.CreateRunspace(remoteHost) : RunspaceFactory.CreateRunspace(); runspace.Open(); try { powershell.InvocationStateChanged += HandlePowerShellInvocationStateChanged; powershell.SetIsNested(false); string script = @" param ($Debugger, $Commands, $output) trap { throw $_ } $Debugger.ProcessCommand($Commands, $output) "; PSDataCollection<PSObject> output = new PSDataCollection<PSObject>(); PSCommand Commands = new PSCommand(powershell.Commands); powershell.Commands.Clear(); powershell.AddScript(script).AddParameter("Debugger", this).AddParameter("Commands", Commands).AddParameter("output", output); ServerPowerShellDriver driver = new ServerPowerShellDriver( powershell, null, true, powershellId, runspacePoolId, runspacePoolDriver, #if !CORECLR // No ApartmentState In CoreCLR apartmentState, #endif hostInfo, streamOptions, addToHistory, runspace, output); driver.Start(); } catch (Exception e) { CommandProcessorBase.CheckForSevereException(e); runspace.Close(); runspace.Dispose(); } }
/// <summary> /// Creates a ServerRemoteHost which is associated with this powershell. /// </summary> /// <param name="powerShellHostInfo">Host information about the host associated /// PowerShell object on the client.</param> /// <param name="runspaceServerRemoteHost">Host associated with the RunspacePool /// on the server.</param> /// <returns>A new ServerRemoteHost for the PowerShell.</returns> internal ServerRemoteHost GetHostAssociatedWithPowerShell( HostInfo powerShellHostInfo, ServerRemoteHost runspaceServerRemoteHost) { HostInfo hostInfo; // If host was null use the runspace's host for this powershell; otherwise, // use the HostInfo to create a proxy host of the powershell's host. if (powerShellHostInfo.UseRunspaceHost) { hostInfo = runspaceServerRemoteHost.HostInfo; } else { hostInfo = powerShellHostInfo; } // If the host was not null on the client, then the PowerShell object should // get a brand spanking new host. return new ServerRemoteHost(_clientRunspacePoolId, _clientPowerShellId, hostInfo, _transportManager, runspaceServerRemoteHost.Runspace, runspaceServerRemoteHost as ServerDriverRemoteHost); }
/// <summary> /// Default constructor for creating ServerPowerShellDrivers /// </summary> /// <param name="powershell">decoded powershell object</param> /// <param name="extraPowerShell">extra pipeline to be run after <paramref name="powershell"/> completes</param> /// <param name="noInput">whether there is input for this powershell</param> /// <param name="clientPowerShellId">the client powershell id</param> /// <param name="clientRunspacePoolId">the client runspacepool id</param> /// <param name="runspacePoolDriver">runspace pool driver /// which is creating this powershell driver</param> /// <param name="apartmentState">apartment state for this powershell</param> /// <param name="hostInfo">host info using which the host for /// this powershell will be constructed</param> /// <param name="streamOptions">serialization options for the streams in this powershell</param> /// <param name="addToHistory"> /// true if the command is to be added to history list of the runspace. false, otherwise. /// </param> /// <param name="rsToUse"> /// If not null, this Runspace will be used to invoke Powershell. /// If null, the RunspacePool pointed by <paramref name="runspacePoolDriver"/> will be used. /// </param> /// <param name="output"> /// If not null, this is used as another source of output sent to the client. /// </param> internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse, PSDataCollection<PSObject> output) #endif { InstanceId = clientPowerShellId; RunspacePoolId = clientRunspacePoolId; RemoteStreamOptions = streamOptions; #if !CORECLR // No ApartmentState In CoreCLR this.apartmentState = apartmentState; #endif LocalPowerShell = powershell; _extraPowerShell = extraPowerShell; _localPowerShellOutput = new PSDataCollection<PSObject>(); _noInput = noInput; _addToHistory = addToHistory; _psDriverInvoker = runspacePoolDriver; DataStructureHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, RemoteStreamOptions, LocalPowerShell); _remoteHost = DataStructureHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); if (!noInput) { InputCollection = new PSDataCollection<object>(); InputCollection.ReleaseOnEnumeration = true; InputCollection.IdleEvent += new EventHandler<EventArgs>(HandleIdleEvent); } RegisterPipelineOutputEventHandlers(_localPowerShellOutput); if (LocalPowerShell != null) { RegisterPowerShellEventHandlers(LocalPowerShell); _datasent[0] = false; } if (extraPowerShell != null) { RegisterPowerShellEventHandlers(extraPowerShell); _datasent[1] = false; } RegisterDataStructureHandlerEventHandlers(DataStructureHandler); // set the runspace pool and invoke this powershell if (null != rsToUse) { LocalPowerShell.Runspace = rsToUse; if (extraPowerShell != null) { extraPowerShell.Runspace = rsToUse; } } else { LocalPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell != null) { extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } } if (output != null) { output.DataAdded += (sender, args) => { if (_localPowerShellOutput.IsOpen) { var items = output.ReadAll(); foreach (var item in items) { _localPowerShellOutput.Add(item); } } }; } }