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 ;
215using System ;
316using System . Collections . Generic ;
417using System . ComponentModel . Composition ;
@@ -102,6 +115,7 @@ public SwitchParameter Recurse
102115 /// <summary>
103116 /// ShowSuppressed: Show the suppressed message
104117 /// </summary>
118+ [ Parameter ( Mandatory = false ) ]
105119 [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
106120 public SwitchParameter SuppressedOnly
107121 {
@@ -270,7 +284,28 @@ private void AnalyzeFile(string filePath)
270284 List < SuppressedRecord > suppressed = new List < SuppressedRecord > ( ) ;
271285
272286 // 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+
274309
275310 //Parse the file
276311 if ( File . Exists ( filePath ) )
@@ -327,12 +362,32 @@ private void AnalyzeFile(string filePath)
327362 #region Run ScriptRules
328363 //Trim down to the leaf element of the filePath and pass it to Diagnostic Record
329364 string fileName = System . IO . Path . GetFileName ( filePath ) ;
365+
330366 if ( ScriptAnalyzer . Instance . ScriptRules != null )
331367 {
332368 foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
333369 {
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 ) )
336391 {
337392 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , scriptRule . GetName ( ) ) ) ;
338393
@@ -347,7 +402,6 @@ private void AnalyzeFile(string filePath)
347402 catch ( Exception scriptRuleException )
348403 {
349404 WriteError ( new ErrorRecord ( scriptRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
350- continue ;
351405 }
352406 }
353407 }
@@ -361,8 +415,25 @@ private void AnalyzeFile(string filePath)
361415 {
362416 foreach ( ITokenRule tokenRule in ScriptAnalyzer . Instance . TokenRules )
363417 {
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 ) )
366437 {
367438 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
368439
@@ -377,7 +448,6 @@ private void AnalyzeFile(string filePath)
377448 catch ( Exception tokenRuleException )
378449 {
379450 WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
380- continue ;
381451 }
382452 }
383453 }
@@ -391,8 +461,28 @@ private void AnalyzeFile(string filePath)
391461 // Run DSC Class rule
392462 foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
393463 {
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 ) )
396486 {
397487 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
398488
@@ -407,7 +497,6 @@ private void AnalyzeFile(string filePath)
407497 catch ( Exception dscResourceRuleException )
408498 {
409499 WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
410- continue ;
411500 }
412501 }
413502 }
@@ -435,8 +524,24 @@ private void AnalyzeFile(string filePath)
435524 // Run all DSC Rules
436525 foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
437526 {
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 ) )
440545 {
441546 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
442547
@@ -451,7 +556,6 @@ private void AnalyzeFile(string filePath)
451556 catch ( Exception dscResourceRuleException )
452557 {
453558 WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
454- continue ;
455559 }
456560 }
457561 }
0 commit comments