11
11
//
12
12
13
13
using System . Text . RegularExpressions ;
14
- using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
15
14
using System ;
16
15
using System . Collections . Generic ;
17
- using System . ComponentModel . Composition ;
18
16
using System . Diagnostics . CodeAnalysis ;
19
17
using System . Globalization ;
20
18
using System . Linq ;
21
19
using System . Management . Automation ;
22
20
using System . Management . Automation . Language ;
23
- using System . Resources ;
24
- using System . Threading ;
25
- using System . Reflection ;
26
21
using System . IO ;
27
- using System . Text ;
22
+ using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
28
23
29
- namespace Microsoft . Windows . Powershell . ScriptAnalyzer . Commands
24
+ namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
30
25
{
31
26
/// <summary>
32
27
/// InvokeScriptAnalyzerCommand: Cmdlet to statically check PowerShell scripts.
@@ -129,8 +124,8 @@ public SwitchParameter SuppressedOnly
129
124
#region Private Members
130
125
131
126
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 ;
134
129
135
130
#endregion
136
131
@@ -163,9 +158,9 @@ protected override void BeginProcessing()
163
158
}
164
159
else
165
160
{
166
- validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
161
+ validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
167
162
validationResults . Add ( "ValidModPaths" , new List < string > ( ) ) ;
168
- validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
163
+ validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
169
164
}
170
165
171
166
#endregion
@@ -174,7 +169,7 @@ protected override void BeginProcessing()
174
169
175
170
try
176
171
{
177
- if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
172
+ if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
178
173
validationResults [ "ValidModPaths" ] . Count == 0 )
179
174
{
180
175
ScriptAnalyzer . Instance . Initialize ( ) ;
@@ -185,7 +180,7 @@ protected override void BeginProcessing()
185
180
}
186
181
}
187
182
catch ( Exception ex )
188
- {
183
+ {
189
184
ThrowTerminatingError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" , CultureInfo . CurrentCulture ) ,
190
185
ErrorCategory . NotSpecified , this ) ) ;
191
186
}
@@ -228,8 +223,8 @@ private void ProcessPath(string path)
228
223
229
224
if ( path == null )
230
225
{
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 ) ,
233
228
ErrorCategory . InvalidArgument , this ) ) ;
234
229
}
235
230
@@ -315,11 +310,11 @@ private void AnalyzeFile(string filePath)
315
310
else
316
311
{
317
312
ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
318
- string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
313
+ string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
319
314
ErrorCategory . InvalidArgument , filePath ) ) ;
320
315
}
321
316
322
- if ( errors != null && errors . Length > 0 )
317
+ if ( errors != null && errors . Length > 0 )
323
318
{
324
319
foreach ( ParseError error in errors )
325
320
{
@@ -362,7 +357,7 @@ private void AnalyzeFile(string filePath)
362
357
#region Run ScriptRules
363
358
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
364
359
string fileName = System . IO . Path . GetFileName ( filePath ) ;
365
-
360
+
366
361
if ( ScriptAnalyzer . Instance . ScriptRules != null )
367
362
{
368
363
foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
@@ -433,7 +428,7 @@ private void AnalyzeFile(string filePath)
433
428
break ;
434
429
}
435
430
}
436
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
431
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
437
432
{
438
433
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
439
434
@@ -448,7 +443,7 @@ private void AnalyzeFile(string filePath)
448
443
catch ( Exception tokenRuleException )
449
444
{
450
445
WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
451
- }
446
+ }
452
447
}
453
448
}
454
449
}
@@ -458,46 +453,50 @@ private void AnalyzeFile(string filePath)
458
453
#region DSC Resource Rules
459
454
if ( ScriptAnalyzer . Instance . DSCResourceRules != null )
460
455
{
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 ) )
463
458
{
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 )
468
461
{
469
- if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
462
+ bool includeRegexMatch = false ;
463
+ bool excludeRegexMatch = false ;
464
+
465
+ foreach ( Regex include in includeRegexList )
470
466
{
471
- includeRegexMatch = true ;
472
- break ;
467
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
468
+ {
469
+ includeRegexMatch = true ;
470
+ break ;
471
+ }
473
472
}
474
- }
475
473
476
- foreach ( Regex exclude in excludeRegexList )
477
- {
478
- if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
474
+ foreach ( Regex exclude in excludeRegexList )
479
475
{
480
- excludeRegexMatch = true ;
481
- break ;
476
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
477
+ {
478
+ excludeRegexMatch = true ;
479
+ break ;
480
+ }
482
481
}
483
- }
484
-
485
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
486
- {
487
- WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
488
482
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 ) )
492
484
{
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
+ }
496
499
}
497
- catch ( Exception dscResourceRuleException )
498
- {
499
- WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
500
- }
501
500
}
502
501
}
503
502
@@ -543,7 +542,7 @@ private void AnalyzeFile(string filePath)
543
542
}
544
543
}
545
544
546
- }
545
+ }
547
546
}
548
547
#endregion
549
548
@@ -607,4 +606,4 @@ private void AnalyzeFile(string filePath)
607
606
608
607
#endregion
609
608
}
610
- }
609
+ }
0 commit comments