@@ -208,7 +208,6 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
208208 PSCommand psCommand ,
209209 bool sendOutputToHost = false )
210210 {
211- Pipeline nestedPipeline = null ;
212211 RunspaceHandle runspaceHandle = null ;
213212 IEnumerable < TResult > executionResult = null ;
214213
@@ -255,17 +254,10 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
255254 "Attempting to execute nested pipeline command(s):\r \n \r \n {0}" ,
256255 GetStringForPSCommand ( psCommand ) ) ) ;
257256
258- nestedPipeline = this . currentRunspace . CreateNestedPipeline ( ) ;
259- foreach ( var command in psCommand . Commands )
260- {
261- nestedPipeline . Commands . Add ( command ) ;
262- }
263-
264257 executionResult =
265- nestedPipeline
266- . Invoke ( )
267- . Select ( pso => pso . BaseObject )
268- . Cast < TResult > ( ) ;
258+ this . ExecuteCommandInDebugger < TResult > (
259+ psCommand ,
260+ sendOutputToHost ) ;
269261 }
270262 else
271263 {
@@ -337,7 +329,7 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
337329 {
338330 this . WritePromptWithRunspace ( runspaceHandle . Runspace ) ;
339331 }
340- else if ( nestedPipeline != null )
332+ else
341333 {
342334 this . WritePromptWithNestedPipeline ( ) ;
343335 }
@@ -348,10 +340,6 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
348340 {
349341 runspaceHandle . Dispose ( ) ;
350342 }
351- else if ( nestedPipeline != null )
352- {
353- nestedPipeline . Dispose ( ) ;
354- }
355343 }
356344 }
357345
@@ -571,6 +559,73 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e
571559
572560 #region Private Methods
573561
562+ private IEnumerable < TResult > ExecuteCommandInDebugger < TResult > ( PSCommand psCommand , bool sendOutputToHost )
563+ {
564+ IEnumerable < TResult > executionResult = null ;
565+
566+ if ( PowerShellVersion >= new Version ( 4 , 0 ) )
567+ {
568+ #if PowerShellv4 || PowerShellv5
569+ PSDataCollection < PSObject > outputCollection = new PSDataCollection < PSObject > ( ) ;
570+
571+ if ( sendOutputToHost )
572+ {
573+ outputCollection . DataAdded +=
574+ ( obj , e ) =>
575+ {
576+ for ( int i = e . Index ; i < outputCollection . Count ; i ++ )
577+ {
578+ this . WriteOutput ( outputCollection [ i ] . ToString ( ) , true ) ;
579+ }
580+ } ;
581+ }
582+
583+ DebuggerCommandResults commandResults =
584+ this . currentRunspace . Debugger . ProcessCommand (
585+ psCommand ,
586+ outputCollection ) ;
587+
588+ // If the command was a debugger action, run it
589+ if ( commandResults . ResumeAction . HasValue )
590+ {
591+ this . ResumeDebugger ( commandResults . ResumeAction . Value ) ;
592+ }
593+
594+ executionResult =
595+ outputCollection
596+ . Select ( pso => pso . BaseObject )
597+ . Cast < TResult > ( ) ;
598+ #endif
599+ }
600+ else
601+ {
602+ using ( var nestedPipeline = this . currentRunspace . CreateNestedPipeline ( ) )
603+ {
604+ foreach ( var command in psCommand . Commands )
605+ {
606+ nestedPipeline . Commands . Add ( command ) ;
607+ }
608+
609+ executionResult =
610+ nestedPipeline
611+ . Invoke ( )
612+ . Select ( pso => pso . BaseObject )
613+ . Cast < TResult > ( ) ;
614+ }
615+
616+ // Write the output to the host if necessary
617+ if ( sendOutputToHost )
618+ {
619+ foreach ( var line in executionResult )
620+ {
621+ this . WriteOutput ( line . ToString ( ) , true ) ;
622+ }
623+ }
624+ }
625+
626+ return executionResult ;
627+ }
628+
574629 private void WriteOutput ( string outputString , bool includeNewLine )
575630 {
576631 ( ( IConsoleHost ) this ) . WriteOutput (
@@ -847,10 +902,9 @@ private void WritePromptWithNestedPipeline()
847902 . Cast < string > ( )
848903 . FirstOrDefault ( ) ;
849904 } ) ;
850-
851905 }
852906 }
853-
907+
854908 #endregion
855909
856910 #region Events
0 commit comments