@@ -11,6 +11,7 @@ namespace Microsoft.PowerShell.EditorServices.Console
11
11
using System . Management . Automation ;
12
12
using System . Management . Automation . Host ;
13
13
using System . Management . Automation . Runspaces ;
14
+ using System . Text ;
14
15
using System . Threading ;
15
16
16
17
/// <summary>
@@ -152,6 +153,9 @@ private void Initialize(Runspace initialRunspace)
152
153
this . powerShell . InvocationStateChanged += powerShell_InvocationStateChanged ;
153
154
this . powerShell . Runspace = this . currentRunspace ;
154
155
156
+ // TODO: Should this be configurable?
157
+ this . SetExecutionPolicy ( ExecutionPolicy . RemoteSigned ) ;
158
+
155
159
this . SessionState = PowerShellSessionState . Ready ;
156
160
}
157
161
@@ -192,6 +196,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
192
196
if ( Thread . CurrentThread . ManagedThreadId != this . pipelineThreadId &&
193
197
this . pipelineExecutionTask != null )
194
198
{
199
+ Logger . Write ( LogLevel . Verbose , "Passing command execution to pipeline thread." ) ;
200
+
195
201
PipelineExecutionRequest < TResult > executionRequest =
196
202
new PipelineExecutionRequest < TResult > (
197
203
this , psCommand , sendOutputToHost ) ;
@@ -205,23 +211,29 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
205
211
}
206
212
else
207
213
{
208
- // Instruct PowerShell to send output and errors to the host
209
- if ( sendOutputToHost )
214
+ try
210
215
{
211
- psCommand . Commands [ 0 ] . MergeMyResults (
212
- PipelineResultTypes . Error ,
213
- PipelineResultTypes . Output ) ;
216
+ // Instruct PowerShell to send output and errors to the host
217
+ if ( sendOutputToHost )
218
+ {
219
+ psCommand . Commands [ 0 ] . MergeMyResults (
220
+ PipelineResultTypes . Error ,
221
+ PipelineResultTypes . Output ) ;
214
222
215
- psCommand . Commands . Add (
216
- this . GetOutputCommand (
217
- endOfStatement : false ) ) ;
218
- }
223
+ psCommand . Commands . Add (
224
+ this . GetOutputCommand (
225
+ endOfStatement : false ) ) ;
226
+ }
219
227
220
- try
221
- {
222
228
if ( this . currentRunspace . RunspaceAvailability == RunspaceAvailability . AvailableForNestedCommand ||
223
229
this . debuggerStoppedTask != null )
224
230
{
231
+ Logger . Write (
232
+ LogLevel . Verbose ,
233
+ string . Format (
234
+ "Attempting to execute nested pipeline command(s):\r \n \r \n {0}" ,
235
+ GetStringForPSCommand ( psCommand ) ) ) ;
236
+
225
237
using ( Pipeline pipeline = this . currentRunspace . CreateNestedPipeline ( ) )
226
238
{
227
239
foreach ( var command in psCommand . Commands )
@@ -240,6 +252,12 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
240
252
}
241
253
else
242
254
{
255
+ Logger . Write (
256
+ LogLevel . Verbose ,
257
+ string . Format (
258
+ "Attempting to execute command(s):\r \n \r \n {0}" ,
259
+ GetStringForPSCommand ( psCommand ) ) ) ;
260
+
243
261
// Set the runspace
244
262
var runspaceHandle = await this . GetRunspaceHandle ( ) ;
245
263
if ( runspaceHandle . Runspace . RunspaceAvailability != RunspaceAvailability . AvailableForNestedCommand )
@@ -264,14 +282,30 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
264
282
265
283
runspaceHandle . Dispose ( ) ;
266
284
285
+ if ( this . powerShell . HadErrors )
286
+ {
287
+ // TODO: Find a good way to extract errors!
288
+ Logger . Write (
289
+ LogLevel . Error ,
290
+ "Execution completed with errors." ) ;
291
+ }
292
+ else
293
+ {
294
+ Logger . Write (
295
+ LogLevel . Verbose ,
296
+ "Execution completed successfully." ) ;
297
+ }
298
+
267
299
bool hadErrors = this . powerShell . HadErrors ;
268
300
return taskResult ;
269
301
}
270
302
}
271
303
catch ( RuntimeException e )
272
304
{
273
305
// TODO: Return an error
274
- string boo = e . Message ;
306
+ Logger . Write (
307
+ LogLevel . Error ,
308
+ "Exception occurred while attempting to execute command:\r \n \r \n " + e . ToString ( ) ) ;
275
309
}
276
310
}
277
311
@@ -307,13 +341,16 @@ public async Task ExecuteScriptAtPath(string scriptPath)
307
341
308
342
public void AbortExecution ( )
309
343
{
310
- // TODO: Verify this behavior
344
+ Logger . Write ( LogLevel . Verbose , "Execution abort requested..." ) ;
345
+
311
346
this . powerShell . BeginStop ( null , null ) ;
312
347
this . ResumeDebugger ( DebuggerResumeAction . Stop ) ;
313
348
}
314
349
315
350
public void BreakExecution ( )
316
351
{
352
+ Logger . Write ( LogLevel . Verbose , "Debugger break requested..." ) ;
353
+
317
354
this . currentRunspace . Debugger . SetDebuggerStepMode ( true ) ;
318
355
}
319
356
@@ -383,6 +420,13 @@ internal void ReleaseRunspaceHandle(RunspaceHandle runspaceHandle)
383
420
384
421
private void OnSessionStateChanged ( object sender , SessionStateChangedEventArgs e )
385
422
{
423
+ Logger . Write (
424
+ LogLevel . Verbose ,
425
+ string . Format (
426
+ "Session state changed --\r \n \r \n Old state: {0}\r \n New state: {1}" ,
427
+ this . SessionState . ToString ( ) ,
428
+ e . NewSessionState . ToString ( ) ) ) ;
429
+
386
430
this . SessionState = e . NewSessionState ;
387
431
388
432
if ( this . SessionStateChanged != null )
@@ -472,6 +516,66 @@ private Command GetOutputCommand(bool endOfStatement)
472
516
return outputCommand ;
473
517
}
474
518
519
+ private static string GetStringForPSCommand ( PSCommand psCommand )
520
+ {
521
+ StringBuilder stringBuilder = new StringBuilder ( ) ;
522
+
523
+ foreach ( var command in psCommand . Commands )
524
+ {
525
+ stringBuilder . Append ( " " ) ;
526
+ stringBuilder . AppendLine ( command . ToString ( ) ) ;
527
+ }
528
+
529
+ return stringBuilder . ToString ( ) ;
530
+ }
531
+
532
+ private void SetExecutionPolicy ( ExecutionPolicy desiredExecutionPolicy )
533
+ {
534
+ var currentPolicy = ExecutionPolicy . Undefined ;
535
+
536
+ // Get the current execution policy so that we don't set it higher than it already is
537
+ this . powerShell . Commands . AddCommand ( "Get-ExecutionPolicy" ) ;
538
+
539
+ var result = this . powerShell . Invoke < ExecutionPolicy > ( ) ;
540
+ if ( result . Count > 0 )
541
+ {
542
+ currentPolicy = result . FirstOrDefault ( ) ;
543
+ }
544
+
545
+ if ( desiredExecutionPolicy < currentPolicy ||
546
+ desiredExecutionPolicy == ExecutionPolicy . Bypass ||
547
+ currentPolicy == ExecutionPolicy . Undefined )
548
+ {
549
+ Logger . Write (
550
+ LogLevel . Verbose ,
551
+ string . Format (
552
+ "Setting execution policy:\r \n Current = ExecutionPolicy.{0}\r \n Desired = ExecutionPolicy.{1}" ,
553
+ currentPolicy ,
554
+ desiredExecutionPolicy ) ) ;
555
+
556
+ this . powerShell . Commands . Clear ( ) ;
557
+ this . powerShell
558
+ . AddCommand ( "Set-ExecutionPolicy" )
559
+ . AddParameter ( "ExecutionPolicy" , desiredExecutionPolicy )
560
+ . AddParameter ( "Scope" , ExecutionPolicyScope . Process )
561
+ . AddParameter ( "Force" ) ;
562
+
563
+ this . powerShell . Invoke ( ) ;
564
+ this . powerShell . Commands . Clear ( ) ;
565
+
566
+ // TODO: Ensure there were no errors?
567
+ }
568
+ else
569
+ {
570
+ Logger . Write (
571
+ LogLevel . Verbose ,
572
+ string . Format (
573
+ "Current execution policy: ExecutionPolicy.{0}" ,
574
+ currentPolicy ) ) ;
575
+
576
+ }
577
+ }
578
+
475
579
#endregion
476
580
477
581
#region Events
@@ -482,6 +586,8 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
482
586
{
483
587
if ( ! this . isStopping )
484
588
{
589
+ Logger . Write ( LogLevel . Verbose , "Debugger stopped execution." ) ;
590
+
485
591
// Set the task so a result can be set
486
592
this . debuggerStoppedTask =
487
593
new TaskCompletionSource < DebuggerResumeAction > ( ) ;
@@ -499,6 +605,8 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
499
605
this . DebuggerStop ( sender , e ) ;
500
606
}
501
607
608
+ Logger . Write ( LogLevel . Verbose , "Starting pipeline thread message loop..." ) ;
609
+
502
610
while ( true )
503
611
{
504
612
int taskIndex =
@@ -509,17 +617,23 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
509
617
if ( taskIndex == 0 )
510
618
{
511
619
e . ResumeAction = this . debuggerStoppedTask . Task . Result ;
620
+ Logger . Write ( LogLevel . Verbose , "Received debugger resume action " + e . ResumeAction . ToString ( ) ) ;
621
+
512
622
break ;
513
623
}
514
624
else if ( taskIndex == 1 )
515
625
{
626
+ Logger . Write ( LogLevel . Verbose , "Received pipeline thread execution request." ) ;
627
+
516
628
IPipelineExecutionRequest executionRequest =
517
629
this . pipelineExecutionTask . Task . Result ;
518
630
519
631
this . pipelineExecutionTask = new TaskCompletionSource < IPipelineExecutionRequest > ( ) ;
520
632
521
633
executionRequest . Execute ( ) . Wait ( ) ;
522
634
635
+ Logger . Write ( LogLevel . Verbose , "Pipeline thread execution completed." ) ;
636
+
523
637
this . pipelineResultTask . SetResult ( executionRequest ) ;
524
638
}
525
639
else
0 commit comments