1
- using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
1
+ //
2
+ // Copyright (c) Microsoft Corporation.
3
+ //
4
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
+ // THE SOFTWARE.
11
+ //
12
+
13
+ using System . Text . RegularExpressions ;
14
+ using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
2
15
using System ;
3
16
using System . Collections . Generic ;
4
17
using System . ComponentModel . Composition ;
@@ -102,6 +115,7 @@ public SwitchParameter Recurse
102
115
/// <summary>
103
116
/// ShowSuppressed: Show the suppressed message
104
117
/// </summary>
118
+ [ Parameter ( Mandatory = false ) ]
105
119
[ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
106
120
public SwitchParameter SuppressedOnly
107
121
{
@@ -270,7 +284,28 @@ private void AnalyzeFile(string filePath)
270
284
List < SuppressedRecord > suppressed = new List < SuppressedRecord > ( ) ;
271
285
272
286
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
273
- List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
287
+ List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
288
+
289
+ //Check wild card input for the Include/ExcludeRules and create regex match patterns
290
+ List < Regex > includeRegexList = new List < Regex > ( ) ;
291
+ List < Regex > excludeRegexList = new List < Regex > ( ) ;
292
+ if ( includeRule != null )
293
+ {
294
+ foreach ( string rule in includeRule )
295
+ {
296
+ Regex includeRegex = new Regex ( String . Format ( "^{0}$" , Regex . Escape ( rule ) . Replace ( @"\*" , ".*" ) ) , RegexOptions . IgnoreCase ) ;
297
+ includeRegexList . Add ( includeRegex ) ;
298
+ }
299
+ }
300
+ if ( excludeRule != null )
301
+ {
302
+ foreach ( string rule in excludeRule )
303
+ {
304
+ Regex excludeRegex = new Regex ( String . Format ( "^{0}$" , Regex . Escape ( rule ) . Replace ( @"\*" , ".*" ) ) , RegexOptions . IgnoreCase ) ;
305
+ excludeRegexList . Add ( excludeRegex ) ;
306
+ }
307
+ }
308
+
274
309
275
310
//Parse the file
276
311
if ( File . Exists ( filePath ) )
@@ -327,12 +362,32 @@ private void AnalyzeFile(string filePath)
327
362
#region Run ScriptRules
328
363
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
329
364
string fileName = System . IO . Path . GetFileName ( filePath ) ;
365
+
330
366
if ( ScriptAnalyzer . Instance . ScriptRules != null )
331
367
{
332
368
foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
333
369
{
334
- if ( ( includeRule == null || includeRule . Contains ( scriptRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
335
- ( excludeRule == null || ! excludeRule . Contains ( scriptRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
370
+ bool includeRegexMatch = false ;
371
+ bool excludeRegexMatch = false ;
372
+ foreach ( Regex include in includeRegexList )
373
+ {
374
+ if ( include . IsMatch ( scriptRule . GetName ( ) ) )
375
+ {
376
+ includeRegexMatch = true ;
377
+ break ;
378
+ }
379
+ }
380
+
381
+ foreach ( Regex exclude in excludeRegexList )
382
+ {
383
+ if ( exclude . IsMatch ( scriptRule . GetName ( ) ) )
384
+ {
385
+ excludeRegexMatch = true ;
386
+ break ;
387
+ }
388
+ }
389
+
390
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
336
391
{
337
392
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , scriptRule . GetName ( ) ) ) ;
338
393
@@ -347,7 +402,6 @@ private void AnalyzeFile(string filePath)
347
402
catch ( Exception scriptRuleException )
348
403
{
349
404
WriteError ( new ErrorRecord ( scriptRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
350
- continue ;
351
405
}
352
406
}
353
407
}
@@ -361,8 +415,25 @@ private void AnalyzeFile(string filePath)
361
415
{
362
416
foreach ( ITokenRule tokenRule in ScriptAnalyzer . Instance . TokenRules )
363
417
{
364
- if ( ( includeRule == null || includeRule . Contains ( tokenRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
365
- ( excludeRule == null || ! excludeRule . Contains ( tokenRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
418
+ bool includeRegexMatch = false ;
419
+ bool excludeRegexMatch = false ;
420
+ foreach ( Regex include in includeRegexList )
421
+ {
422
+ if ( include . IsMatch ( tokenRule . GetName ( ) ) )
423
+ {
424
+ includeRegexMatch = true ;
425
+ break ;
426
+ }
427
+ }
428
+ foreach ( Regex exclude in excludeRegexList )
429
+ {
430
+ if ( exclude . IsMatch ( tokenRule . GetName ( ) ) )
431
+ {
432
+ excludeRegexMatch = true ;
433
+ break ;
434
+ }
435
+ }
436
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
366
437
{
367
438
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
368
439
@@ -377,7 +448,6 @@ private void AnalyzeFile(string filePath)
377
448
catch ( Exception tokenRuleException )
378
449
{
379
450
WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
380
- continue ;
381
451
}
382
452
}
383
453
}
@@ -391,8 +461,28 @@ private void AnalyzeFile(string filePath)
391
461
// Run DSC Class rule
392
462
foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
393
463
{
394
- if ( ( includeRule == null || includeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
395
- ( excludeRule == null || ! excludeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
464
+ bool includeRegexMatch = false ;
465
+ bool excludeRegexMatch = false ;
466
+
467
+ foreach ( Regex include in includeRegexList )
468
+ {
469
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
470
+ {
471
+ includeRegexMatch = true ;
472
+ break ;
473
+ }
474
+ }
475
+
476
+ foreach ( Regex exclude in excludeRegexList )
477
+ {
478
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
479
+ {
480
+ excludeRegexMatch = true ;
481
+ break ;
482
+ }
483
+ }
484
+
485
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
396
486
{
397
487
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
398
488
@@ -407,7 +497,6 @@ private void AnalyzeFile(string filePath)
407
497
catch ( Exception dscResourceRuleException )
408
498
{
409
499
WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
410
- continue ;
411
500
}
412
501
}
413
502
}
@@ -435,8 +524,24 @@ private void AnalyzeFile(string filePath)
435
524
// Run all DSC Rules
436
525
foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
437
526
{
438
- if ( ( includeRule == null || includeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
439
- ( excludeRule == null || ! excludeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
527
+ bool includeRegexMatch = false ;
528
+ bool excludeRegexMatch = false ;
529
+ foreach ( Regex include in includeRegexList )
530
+ {
531
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
532
+ {
533
+ includeRegexMatch = true ;
534
+ break ;
535
+ }
536
+ }
537
+ foreach ( Regex exclude in excludeRegexList )
538
+ {
539
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
540
+ {
541
+ excludeRegexMatch = true ;
542
+ }
543
+ }
544
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
440
545
{
441
546
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
442
547
@@ -451,7 +556,6 @@ private void AnalyzeFile(string filePath)
451
556
catch ( Exception dscResourceRuleException )
452
557
{
453
558
WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
454
- continue ;
455
559
}
456
560
}
457
561
}
0 commit comments