@@ -503,18 +503,8 @@ private void Run()
503
503
( PowerShell pwsh , RunspaceInfo localRunspaceInfo , EngineIntrinsics engineIntrinsics ) = CreateInitialPowerShellSession ( ) ;
504
504
_mainRunspaceEngineIntrinsics = engineIntrinsics ;
505
505
_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 ) ;
518
508
}
519
509
catch ( Exception e )
520
510
{
@@ -1015,7 +1005,9 @@ private static PowerShell CreateNestedPowerShell(RunspaceInfo currentRunspace)
1015
1005
// PowerShell.CreateNestedPowerShell() sets IsNested but not IsChild
1016
1006
// This means it throws due to the parent pipeline not running...
1017
1007
// 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 ;
1019
1011
}
1020
1012
1021
1013
private static PowerShell CreatePowerShellForRunspace ( Runspace runspace )
0 commit comments