Exemplo n.º 1
0
        /// <summary>
        /// Runs no command but allows the PowerShell object on the client
        /// to complete.  This is used for running "virtual" remote debug
        /// commands that sets debugger state but doesn't run any command
        /// on the server runspace.
        /// </summary>
        /// <param name="output">The output from preprocessing that we want to send to the client.</param>
        internal void RunNoOpCommand(IReadOnlyCollection <object> output)
        {
            if (LocalPowerShell != null)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(
                    (state) =>
                {
                    LocalPowerShell.SetStateChanged(
                        new PSInvocationStateInfo(
                            PSInvocationState.Running, null));

                    foreach (var item in output)
                    {
                        if (item != null)
                        {
                            _localPowerShellOutput.Add(PSObject.AsPSObject(item));
                        }
                    }

                    LocalPowerShell.SetStateChanged(
                        new PSInvocationStateInfo(
                            PSInvocationState.Completed, null));
                });
            }
        }
Exemplo n.º 2
0
        /// <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 (rsToUse != null)
            {
                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);
                        }
                    }
                };
            }
        }