Skip to content

Commit 6300b22

Browse files
committed
Merge pull request #237 from PowerShell/master
Take latest Master to Development
2 parents e050eb3 + 76200c9 commit 6300b22

File tree

86 files changed

+479
-527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+479
-527
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using Microsoft.PowerShell.Commands;
14-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1513
using System;
1614
using System.Collections.Generic;
1715
using System.Diagnostics.CodeAnalysis;
1816
using System.Globalization;
1917
using System.Linq;
2018
using System.Management.Automation;
19+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
2120

22-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
21+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
2322
{
2423
/// <summary>
2524
/// GetScriptAnalyzerRuleCommand: Cmdlet to list all the analyzer rule names and descriptions.

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,17 @@
1111
//
1212

1313
using System.Text.RegularExpressions;
14-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1514
using System;
1615
using System.Collections.Generic;
17-
using System.ComponentModel.Composition;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.Globalization;
2018
using System.Linq;
2119
using System.Management.Automation;
2220
using System.Management.Automation.Language;
23-
using System.Resources;
24-
using System.Threading;
25-
using System.Reflection;
2621
using 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+
}

Engine/Generic/AvoidCmdletGeneric.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
using System;
1414
using System.Collections.Generic;
1515
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1816
using System.Management.Automation.Language;
1917

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
18+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2119
{
2220
/// <summary>
2321
/// Represents an abstract class for rule that checks whether the script
@@ -38,7 +36,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3836
// Finds all CommandAsts.
3937
IEnumerable<Ast> commandAsts = ast.FindAll(testAst => testAst is CommandAst, true);
4038

41-
List<String> cmdletNameAndAliases = Microsoft.Windows.Powershell.ScriptAnalyzer.Helper.Instance.CmdletNameAndAliases(GetCmdletName());
39+
List<String> cmdletNameAndAliases = Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.Instance.CmdletNameAndAliases(GetCmdletName());
4240

4341
// Iterrates all CommandAsts and check the command name.
4442
foreach (CommandAst cmdAst in commandAsts)

Engine/Generic/AvoidParameterGeneric.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1815
using System.Management.Automation.Language;
1916

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
17+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2118
{
2219
/// <summary>
2320
/// Represents an abstract class for rule that checks that

Engine/Generic/DiagnosticRecord.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1813
using System.Management.Automation.Language;
1914

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
15+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2116
{
2217
/// <summary>
2318
/// Represents a result from a PSScriptAnalyzer rule.

Engine/Generic/ExternalRule.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1813

19-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
14+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2015
{
2116
internal class ExternalRule : IExternalRule
2217
{

Engine/Generic/IDSCResourceRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using System.Collections.Generic;
1414
using System.Management.Automation.Language;
1515

16-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
16+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1717
{
1818
/// <summary>
1919
/// Represents an interface for a DSC rule that analyzes a DSC resource

Engine/Generic/IExternalRule.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1813

19-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
14+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2015
{
2116
/// <summary>
2217
/// Represents an interface for an external analyzer rule.

Engine/Generic/ILogger.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
//
1212

1313
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
18-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Commands;
14+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands;
1915

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
16+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2117
{
2218
/// <summary>
2319
/// ILogger: An interface for a PSScriptAnalyzer logger to output the results of PSScriptAnalyzer rules.

Engine/Generic/IRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
13+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1414
{
1515
/// <summary>
1616
/// An interface for an analyzer rule that analyzes the Ast.

Engine/Generic/IScriptRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Threading.Tasks;
1818
using System.Management.Automation.Language;
1919

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
20+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2121
{
2222
/// <summary>
2323
/// Represents an interface for an analyzer rule that analyzes the Ast.

Engine/Generic/ITokenRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using System.Collections.Generic;
1414
using System.Management.Automation.Language;
1515

16-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
16+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1717
{
1818
/// <summary>
1919
/// Represents an interface for an analyzer rule that analyzes the tokens of a script.

Engine/Generic/LoggerInfo.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1813
using System.Diagnostics.CodeAnalysis;
1914

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
15+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2116
{
2217
/// <summary>
2318
/// Represents an internal class to properly display the name and description of a logger.

Engine/Generic/RuleInfo.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
1813
using System.Diagnostics.CodeAnalysis;
1914

20-
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
15+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2116
{
2217
/// <summary>
2318
/// Represents an internal class to properly display the name and description of a rule.

0 commit comments

Comments
 (0)