@@ -34,6 +34,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
3434
3535 #region Private variables
3636 List < string > processedPaths ;
37+ private int totalDiagnosticCount = 0 ;
3738 #endregion // Private variables
3839
3940 #region Parameters
@@ -412,6 +413,10 @@ protected override void EndProcessing()
412413 {
413414 ScriptAnalyzer . Instance . CleanUp ( ) ;
414415 base . EndProcessing ( ) ;
416+
417+ if ( EnableExit ) {
418+ this . Host . SetShouldExit ( totalDiagnosticCount ) ;
419+ }
415420 }
416421
417422 protected override void StopProcessing ( )
@@ -426,7 +431,10 @@ protected override void StopProcessing()
426431
427432 private void ProcessInput ( )
428433 {
429- WriteToOutput ( RunAnalysis ( ) ) ;
434+ // collect to array to avoid double enumeration
435+ var diagnosticRecords = RunAnalysis ( ) . ToArray ( ) ;
436+ WriteToOutput ( diagnosticRecords ) ;
437+ totalDiagnosticCount += diagnosticRecords . Length ;
430438 }
431439
432440 private IEnumerable < DiagnosticRecord > RunAnalysis ( )
@@ -454,7 +462,7 @@ private IEnumerable<DiagnosticRecord> RunAnalysis()
454462 return diagnostics ;
455463 }
456464
457- private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
465+ private void WriteToOutput ( DiagnosticRecord [ ] diagnosticRecords )
458466 {
459467 foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
460468 {
@@ -507,11 +515,6 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
507515 }
508516 }
509517 }
510-
511- if ( EnableExit . IsPresent )
512- {
513- this . Host . SetShouldExit ( diagnosticRecords . Count ( ) ) ;
514- }
515518 }
516519
517520 private void ProcessPath ( )
@@ -535,4 +538,4 @@ private bool OverrideSwitchParam(bool paramValue, string paramName)
535538
536539 #endregion // Private Methods
537540 }
538- }
541+ }
0 commit comments