1111//
1212
1313using System . Text . RegularExpressions ;
14- using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
1514using System ;
1615using System . Collections . Generic ;
17- using System . ComponentModel . Composition ;
1816using System . Diagnostics . CodeAnalysis ;
1917using System . Globalization ;
2018using System . Linq ;
2119using System . Management . Automation ;
2220using System . Management . Automation . Language ;
23- using System . Resources ;
24- using System . Threading ;
25- using System . Reflection ;
2621using System . IO ;
27- using System . Text ;
22+ using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
2823
29- namespace Microsoft . Windows . Powershell . ScriptAnalyzer . Commands
24+ namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
3025{
3126 /// <summary>
3227 /// InvokeScriptAnalyzerCommand: Cmdlet to statically check PowerShell scripts.
@@ -129,8 +124,8 @@ public SwitchParameter SuppressedOnly
129124 #region Private Members
130125
131126 Dictionary < string , List < string > > validationResults = new Dictionary < string , List < string > > ( ) ;
132- private ScriptBlockAst ast = null ;
133- private IEnumerable < IRule > rules = null ;
127+ private ScriptBlockAst ast = null ;
128+ private IEnumerable < IRule > rules = null ;
134129
135130 #endregion
136131
@@ -163,9 +158,9 @@ protected override void BeginProcessing()
163158 }
164159 else
165160 {
166- validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
161+ validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
167162 validationResults . Add ( "ValidModPaths" , new List < string > ( ) ) ;
168- validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
163+ validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
169164 }
170165
171166 #endregion
@@ -174,7 +169,7 @@ protected override void BeginProcessing()
174169
175170 try
176171 {
177- if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
172+ if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
178173 validationResults [ "ValidModPaths" ] . Count == 0 )
179174 {
180175 ScriptAnalyzer . Instance . Initialize ( ) ;
@@ -185,7 +180,7 @@ protected override void BeginProcessing()
185180 }
186181 }
187182 catch ( Exception ex )
188- {
183+ {
189184 ThrowTerminatingError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" , CultureInfo . CurrentCulture ) ,
190185 ErrorCategory . NotSpecified , this ) ) ;
191186 }
@@ -228,8 +223,8 @@ private void ProcessPath(string path)
228223
229224 if ( path == null )
230225 {
231- ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
232- string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
226+ ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
227+ string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
233228 ErrorCategory . InvalidArgument , this ) ) ;
234229 }
235230
@@ -315,11 +310,11 @@ private void AnalyzeFile(string filePath)
315310 else
316311 {
317312 ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
318- string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
313+ string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
319314 ErrorCategory . InvalidArgument , filePath ) ) ;
320315 }
321316
322- if ( errors != null && errors . Length > 0 )
317+ if ( errors != null && errors . Length > 0 )
323318 {
324319 foreach ( ParseError error in errors )
325320 {
@@ -362,7 +357,7 @@ private void AnalyzeFile(string filePath)
362357 #region Run ScriptRules
363358 //Trim down to the leaf element of the filePath and pass it to Diagnostic Record
364359 string fileName = System . IO . Path . GetFileName ( filePath ) ;
365-
360+
366361 if ( ScriptAnalyzer . Instance . ScriptRules != null )
367362 {
368363 foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
@@ -433,7 +428,7 @@ private void AnalyzeFile(string filePath)
433428 break ;
434429 }
435430 }
436- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
431+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
437432 {
438433 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
439434
@@ -448,7 +443,7 @@ private void AnalyzeFile(string filePath)
448443 catch ( Exception tokenRuleException )
449444 {
450445 WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
451- }
446+ }
452447 }
453448 }
454449 }
@@ -458,46 +453,50 @@ private void AnalyzeFile(string filePath)
458453 #region DSC Resource Rules
459454 if ( ScriptAnalyzer . Instance . DSCResourceRules != null )
460455 {
461- // Run DSC Class rule
462- foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
456+ // Invoke AnalyzeDSCClass only if the ast is a class based resource
457+ if ( Helper . Instance . IsDscResourceClassBased ( ast ) )
463458 {
464- bool includeRegexMatch = false ;
465- bool excludeRegexMatch = false ;
466-
467- foreach ( Regex include in includeRegexList )
459+ // Run DSC Class rule
460+ foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
468461 {
469- if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
462+ bool includeRegexMatch = false ;
463+ bool excludeRegexMatch = false ;
464+
465+ foreach ( Regex include in includeRegexList )
470466 {
471- includeRegexMatch = true ;
472- break ;
467+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
468+ {
469+ includeRegexMatch = true ;
470+ break ;
471+ }
473472 }
474- }
475473
476- foreach ( Regex exclude in excludeRegexList )
477- {
478- if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
474+ foreach ( Regex exclude in excludeRegexList )
479475 {
480- excludeRegexMatch = true ;
481- break ;
476+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
477+ {
478+ excludeRegexMatch = true ;
479+ break ;
480+ }
482481 }
483- }
484-
485- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
486- {
487- WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
488482
489- // Ensure that any unhandled errors from Rules are converted to non-terminating errors
490- // We want the Engine to continue functioning even if one or more Rules throws an exception
491- try
483+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
492484 {
493- var records = Helper . Instance . SuppressRule ( dscResourceRule . GetName ( ) , ruleSuppressions , dscResourceRule . AnalyzeDSCClass ( ast , filePath ) . ToList ( ) ) ;
494- diagnostics . AddRange ( records . Item2 ) ;
495- suppressed . AddRange ( records . Item1 ) ;
485+ WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
486+
487+ // Ensure that any unhandled errors from Rules are converted to non-terminating errors
488+ // We want the Engine to continue functioning even if one or more Rules throws an exception
489+ try
490+ {
491+ var records = Helper . Instance . SuppressRule ( dscResourceRule . GetName ( ) , ruleSuppressions , dscResourceRule . AnalyzeDSCClass ( ast , filePath ) . ToList ( ) ) ;
492+ diagnostics . AddRange ( records . Item2 ) ;
493+ suppressed . AddRange ( records . Item1 ) ;
494+ }
495+ catch ( Exception dscResourceRuleException )
496+ {
497+ WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
498+ }
496499 }
497- catch ( Exception dscResourceRuleException )
498- {
499- WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
500- }
501500 }
502501 }
503502
@@ -543,7 +542,7 @@ private void AnalyzeFile(string filePath)
543542 }
544543 }
545544
546- }
545+ }
547546 }
548547 #endregion
549548
@@ -607,4 +606,4 @@ private void AnalyzeFile(string filePath)
607606
608607 #endregion
609608 }
610- }
609+ }
0 commit comments