Beispiel #1
0
        public IEnumerable <TResult> ExecuteCommandInDebugger <TResult>(
            PowerShellContextService powerShellContext,
            Runspace currentRunspace,
            PSCommand psCommand,
            bool sendOutputToHost,
            out DebuggerResumeAction?debuggerResumeAction)
        {
            debuggerResumeAction = null;
            PSDataCollection <PSObject> outputCollection = new PSDataCollection <PSObject>();

            if (sendOutputToHost)
            {
                outputCollection.DataAdded +=
                    (obj, e) =>
                {
                    for (int i = e.Index; i < outputCollection.Count; i++)
                    {
                        powerShellContext.WriteOutput(
                            outputCollection[i].ToString(),
                            true);
                    }
                };
            }

            DebuggerCommandResults commandResults =
                currentRunspace.Debugger.ProcessCommand(
                    psCommand,
                    outputCollection);

            // Pass along the debugger's resume action if the user's
            // command caused one to be returned
            debuggerResumeAction = commandResults.ResumeAction;

            IEnumerable <TResult> results = null;

            if (typeof(TResult) != typeof(PSObject))
            {
                results =
                    outputCollection
                    .Select(pso => pso.BaseObject)
                    .Cast <TResult>();
            }
            else
            {
                results = outputCollection.Cast <TResult>();
            }

            return(results);
        }