/// <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); } } }; } }
internal ServerRemoteHost GetHostAssociatedWithPowerShell( HostInfo powerShellHostInfo, ServerRemoteHost runspaceServerRemoteHost) { return(new ServerRemoteHost(this.clientRunspacePoolId, this.clientPowerShellId, !powerShellHostInfo.UseRunspaceHost ? powerShellHostInfo : runspaceServerRemoteHost.HostInfo, this.transportManager)); }
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, 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; if (!noInput) { this.input = new PSDataCollection <object>(); this.input.ReleaseOnEnumeration = true; } this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(this); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); this.localPowerShellOutput.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleOutputDataAdded); if (this.localPowerShell != null) { this.localPowerShell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(this.HandlePowerShellInvocationStateChanged); this.localPowerShell.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleErrorDataAdded); this.localPowerShell.Streams.Debug.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleDebugAdded); this.localPowerShell.Streams.Verbose.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleVerboseAdded); this.localPowerShell.Streams.Warning.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleWarningAdded); this.localPowerShell.Streams.Progress.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleProgressAdded); } if (extraPowerShell != null) { extraPowerShell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(this.HandlePowerShellInvocationStateChanged); extraPowerShell.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleErrorDataAdded); extraPowerShell.Streams.Debug.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleDebugAdded); extraPowerShell.Streams.Verbose.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleVerboseAdded); extraPowerShell.Streams.Warning.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleWarningAdded); extraPowerShell.Streams.Progress.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleProgressAdded); } 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); if (rsToUse != null) { this.localPowerShell.Runspace = rsToUse; if (extraPowerShell == null) { return; } extraPowerShell.Runspace = rsToUse; } else { this.localPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell == null) { return; } extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } }