@@ -375,64 +375,72 @@ private void HandlePSEventReceived(object sender, PSEventArgs args)
375
375
376
376
private void RegisterPSEditFunction ( RunspaceDetails runspaceDetails )
377
377
{
378
- try
378
+ if ( runspaceDetails . Location == RunspaceLocation . Remote &&
379
+ runspaceDetails . Context == RunspaceContext . Original )
379
380
{
380
- runspaceDetails . Runspace . Events . ReceivedEvents . PSEventReceived += HandlePSEventReceived ;
381
+ try
382
+ {
383
+ runspaceDetails . Runspace . Events . ReceivedEvents . PSEventReceived += HandlePSEventReceived ;
381
384
382
- var createScript =
383
- string . Format (
384
- CreatePSEditFunctionScript ,
385
- ( runspaceDetails . Location == RunspaceLocation . Local &&
386
- runspaceDetails . Context == RunspaceContext . Original )
387
- ? string . Empty : "-Forward" ) ;
385
+ var createScript =
386
+ string . Format (
387
+ CreatePSEditFunctionScript ,
388
+ ( runspaceDetails . Location == RunspaceLocation . Local &&
389
+ runspaceDetails . Context == RunspaceContext . Original )
390
+ ? string . Empty : "-Forward" ) ;
388
391
389
- PSCommand createCommand = new PSCommand ( ) ;
390
- createCommand
391
- . AddScript ( createScript )
392
- . AddParameter ( "PSEditFunction" , PSEditFunctionScript ) ;
392
+ PSCommand createCommand = new PSCommand ( ) ;
393
+ createCommand
394
+ . AddScript ( createScript )
395
+ . AddParameter ( "PSEditFunction" , PSEditFunctionScript ) ;
393
396
394
- if ( runspaceDetails . Context == RunspaceContext . DebuggedRunspace )
395
- {
396
- this . powerShellContext . ExecuteCommand ( createCommand ) . Wait ( ) ;
397
- }
398
- else
399
- {
400
- using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
397
+ if ( runspaceDetails . Context == RunspaceContext . DebuggedRunspace )
401
398
{
402
- powerShell . Runspace = runspaceDetails . Runspace ;
403
- powerShell . Commands = createCommand ;
404
- powerShell . Invoke ( ) ;
399
+ this . powerShellContext . ExecuteCommand ( createCommand ) . Wait ( ) ;
400
+ }
401
+ else
402
+ {
403
+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
404
+ {
405
+ powerShell . Runspace = runspaceDetails . Runspace ;
406
+ powerShell . Commands = createCommand ;
407
+ powerShell . Invoke ( ) ;
408
+ }
405
409
}
406
410
}
407
- }
408
- catch ( RemoteException e )
409
- {
410
- Logger . WriteException ( "Could not create psedit function." , e ) ;
411
+ catch ( RemoteException e )
412
+ {
413
+ Logger . WriteException ( "Could not create psedit function." , e ) ;
414
+ }
411
415
}
412
416
}
413
417
414
418
private void RemovePSEditFunction ( RunspaceDetails runspaceDetails )
415
419
{
416
- try
420
+ if ( runspaceDetails . Location == RunspaceLocation . Remote &&
421
+ runspaceDetails . Context == RunspaceContext . Original )
417
422
{
418
- if ( runspaceDetails . Runspace . Events != null )
423
+ try
419
424
{
420
- runspaceDetails . Runspace . Events . ReceivedEvents . PSEventReceived -= HandlePSEventReceived ;
421
- }
425
+ if ( runspaceDetails . Runspace . Events != null )
426
+ {
427
+ runspaceDetails . Runspace . Events . ReceivedEvents . PSEventReceived -= HandlePSEventReceived ;
428
+ }
422
429
423
- if ( runspaceDetails . Runspace . RunspaceStateInfo . State == RunspaceState . Opened )
424
- {
425
- using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
430
+ if ( runspaceDetails . Runspace . RunspaceStateInfo . State == RunspaceState . Opened )
426
431
{
427
- powerShell . Runspace = runspaceDetails . Runspace ;
428
- powerShell . Commands . AddScript ( RemovePSEditFunctionScript ) ;
429
- powerShell . Invoke ( ) ;
432
+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
433
+ {
434
+ powerShell . Runspace = runspaceDetails . Runspace ;
435
+ powerShell . Commands . AddScript ( RemovePSEditFunctionScript ) ;
436
+ powerShell . Invoke ( ) ;
437
+ }
430
438
}
431
439
}
432
- }
433
- catch ( Exception e ) when ( e is RemoteException || e is PSInvalidOperationException )
434
- {
435
- Logger . WriteException ( "Could not remove psedit function." , e ) ;
440
+ catch ( Exception e ) when ( e is RemoteException || e is PSInvalidOperationException )
441
+ {
442
+ Logger . WriteException ( "Could not remove psedit function." , e ) ;
443
+ }
436
444
}
437
445
}
438
446
0 commit comments