@@ -503,18 +503,8 @@ private void Run()
503503 ( PowerShell pwsh , RunspaceInfo localRunspaceInfo , EngineIntrinsics engineIntrinsics ) = CreateInitialPowerShellSession ( ) ;
504504 _mainRunspaceEngineIntrinsics = engineIntrinsics ;
505505 _localComputerName = localRunspaceInfo . SessionDetails . ComputerName ;
506-
507- // NOTE: In order to support running events registered to PowerShell's OnIdle
508- // handler, we have to have our top-level PowerShell instance be nested (otherwise
509- // we get a PSInvalidOperationException because pipelines cannot be run
510- // concurrently). Specifically this bug cropped up when a profile loaded code which
511- // registered (and subsequently ran) on the OnIdle handler since it was hitting the
512- // non-nested PowerShell instance. So now we just start with a nested instance.
513- // While the PowerShell object is nested, as a frame type, this is our top-level
514- // frame and therefore NOT nested in that sense.
515- PowerShell nestedPwsh = CreateNestedPowerShell ( localRunspaceInfo ) ;
516- _runspaceStack . Push ( new RunspaceFrame ( nestedPwsh . Runspace , localRunspaceInfo ) ) ;
517- PushPowerShellAndRunLoop ( nestedPwsh , PowerShellFrameType . Normal | PowerShellFrameType . Repl , localRunspaceInfo ) ;
506+ _runspaceStack . Push ( new RunspaceFrame ( pwsh . Runspace , localRunspaceInfo ) ) ;
507+ PushPowerShellAndRunLoop ( pwsh , PowerShellFrameType . Normal | PowerShellFrameType . Repl , localRunspaceInfo ) ;
518508 }
519509 catch ( Exception e )
520510 {
@@ -1015,7 +1005,9 @@ private static PowerShell CreateNestedPowerShell(RunspaceInfo currentRunspace)
10151005 // PowerShell.CreateNestedPowerShell() sets IsNested but not IsChild
10161006 // This means it throws due to the parent pipeline not running...
10171007 // So we must use the RunspaceMode.CurrentRunspace option on PowerShell.Create() instead
1018- return PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
1008+ PowerShell pwsh = PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
1009+ pwsh . Runspace . ThreadOptions = PSThreadOptions . UseCurrentThread ;
1010+ return pwsh ;
10191011 }
10201012
10211013 private static PowerShell CreatePowerShellForRunspace ( Runspace runspace )
0 commit comments