Skip to content

Commit 6ea858a

Browse files
committed
Merge pull request #119 from PowerShell/master
Forward Integrate: Master to Development
2 parents 0bade57 + 210c2a3 commit 6ea858a

8 files changed

+195
-57
lines changed

CHANGELOG.MD

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Unreleased (May.7, 2015)
2+
###Features:
3+
- Integrated with waffle.io for Project Management.
4+
- Added documentation for writing script rules.
5+
6+
###Rules:
7+
- AvoidUsingWMICmdlet rule: For PowerShell 3.0 and above, usage of WMI cmdlets is not recommended. This rule is to detect WMI cmdlet usage in scripts that are written for PS 3.0 and above.
8+
- DSCTestsPresent rule: Resource module contains Tests folder with tests for given resource.
9+
- UseOutputTypeCorrectly rule: If we can identify the type of an object that is outputted to the pipeline by a cmdlet, then that type must be listed in the OutputType attribute.
10+
11+
###Fixes:
12+
13+
- PSProvideVerboseMessage only throws warnings in non-advanced functions.
14+
- Fix the issue in importing customized rule
15+
16+
17+
18+
19+
##Relesed on Apr.24, 2015
20+
21+
###Features:
22+
- Finalized three levels of Severity - Error/Warning/Information.
23+
- Improved PSScriptAnalyzer engine behavior: emits non-terminating errors (Ex: for failed ast parse) and continues rule application when running on multiple scripts.
24+
- Added wild card supports for rules in Invoke-ScriptAnalyzer and Get-ScriptAnalyzer. Eg. Invoke-ScriptAnalyzer -IncludeRule PSAvoid* will apply all rules starting with PSAvoid* in built in rule assemblies.
25+
- Added -Severity to Get-ScriptAnalyzerRules. Get-ScriptAnalyzer -Severity will filter rules based on the severity given.
26+
- Added Suppression functionality. Users are now able to specify suppression on certain parts of the scripts by specifying "SupressMessageAttribute" in the scripts. More details and documentations will be coming soon in blog posts. Also comes with this feature is the ability for users to display a list of suppressed messages.
27+
28+
###Rules:
29+
30+
- Added DSC Rules for resources including Parameter validation, Usage of standard DSC functions and return type validation. Rule checkings also support for DSC classes. Built-in DSC rules include:
31+
+ UseStandardDSCFunctionsInResource
32+
+ UseIdenticalParametersDSC
33+
+ UseIdenticalMandatoryParametersDSC
34+
+ ReturnCorrectTypesForDSCFunctions
35+
- Added support in the engine to detect DSC configuration/resource files and disable default rule checkings on DSC configuration and resource files.
36+
- UseShouldProcessForStateChangingFunctions - If an advanced function has Verbs like New/Start/Stop/Restart/Reset/Set- that will change system state, it should support ShouldProcess attribute.
37+
38+
39+
###Fixes:
40+
41+
- Improved heuristics to detect usage of Username and Password instead of PSCredential type.
42+
- Improved accuracy in the detection of uninitialized variables.
43+
- Improved error messages to include error line numbers and file names.
44+
- Identified usage of PSBound parameters and PowerShell supplied variables such as $MyInvocation to avoid unnecessary noise in the results returned by some of the built-in rules.
45+
- Fixed terminating errors including "Illegal characters in Path".
46+

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -502,68 +502,48 @@ private void AnalyzeFile(string filePath)
502502
}
503503

504504
// Check if the supplied artifact is indeed part of the DSC resource
505-
// Step 1: Check if the artifact is under the "DSCResources" folder
506-
DirectoryInfo dscResourceParent = Directory.GetParent(filePath);
507-
if (null != dscResourceParent)
505+
if (Helper.Instance.IsDscResourceModule(filePath))
508506
{
509-
DirectoryInfo dscResourcesFolder = Directory.GetParent(dscResourceParent.ToString());
510-
if (null != dscResourcesFolder)
511-
{
512-
if (String.Equals(dscResourcesFolder.Name, "dscresources",StringComparison.OrdinalIgnoreCase))
507+
// Run all DSC Rules
508+
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
509+
{
510+
bool includeRegexMatch = false;
511+
bool excludeRegexMatch = false;
512+
foreach (Regex include in includeRegexList)
513+
{
514+
if (include.IsMatch(dscResourceRule.GetName()))
515+
{
516+
includeRegexMatch = true;
517+
break;
518+
}
519+
}
520+
foreach (Regex exclude in excludeRegexList)
521+
{
522+
if (exclude.IsMatch(dscResourceRule.GetName()))
523+
{
524+
excludeRegexMatch = true;
525+
}
526+
}
527+
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
513528
{
514-
// Step 2: Ensure there is a Schema.mof in the same folder as the artifact
515-
string schemaMofParentFolder = Directory.GetParent(filePath).ToString();
516-
string[] schemaMofFile = Directory.GetFiles(schemaMofParentFolder, "*.schema.mof");
529+
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));
517530

518-
// Ensure Schema file exists and is the only one in the DSCResource folder
519-
if (schemaMofFile != null && schemaMofFile.Count() == 1)
531+
// Ensure that any unhandled errors from Rules are converted to non-terminating errors
532+
// We want the Engine to continue functioning even if one or more Rules throws an exception
533+
try
520534
{
521-
// Run DSC Rules only on module that matches the schema.mof file name without extension
522-
if (schemaMofFile[0].Replace("schema.mof", "psm1") == filePath)
523-
{
524-
// Run all DSC Rules
525-
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
526-
{
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))
545-
{
546-
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));
547-
548-
// Ensure that any unhandled errors from Rules are converted to non-terminating errors
549-
// We want the Engine to continue functioning even if one or more Rules throws an exception
550-
try
551-
{
552-
var records = Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList());
553-
diagnostics.AddRange(records.Item2);
554-
suppressed.AddRange(records.Item1);
555-
}
556-
catch (Exception dscResourceRuleException)
557-
{
558-
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
559-
}
560-
}
561-
}
562-
}
535+
var records = Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList());
536+
diagnostics.AddRange(records.Item2);
537+
suppressed.AddRange(records.Item1);
538+
}
539+
catch (Exception dscResourceRuleException)
540+
{
541+
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
563542
}
564543
}
565544
}
566-
}
545+
546+
}
567547
}
568548
#endregion
569549

Engine/Helper.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,41 @@ public string GetCmdletNameFromAlias(String Alias)
156156
return String.Empty;
157157
}
158158

159+
/// <summary>
160+
/// Given a file path, checks whether the file is part of a dsc resource module
161+
/// </summary>
162+
/// <param name="fileName"></param>
163+
/// <returns></returns>
164+
public bool IsDscResourceModule(string filePath)
165+
{
166+
DirectoryInfo dscResourceParent = Directory.GetParent(filePath);
167+
if (null != dscResourceParent)
168+
{
169+
DirectoryInfo dscResourcesFolder = Directory.GetParent(dscResourceParent.ToString());
170+
if (null != dscResourcesFolder)
171+
{
172+
if (String.Equals(dscResourcesFolder.Name, "dscresources", StringComparison.OrdinalIgnoreCase))
173+
{
174+
// Step 2: Ensure there is a Schema.mof in the same folder as the artifact
175+
string schemaMofParentFolder = Directory.GetParent(filePath).ToString();
176+
string[] schemaMofFile = Directory.GetFiles(schemaMofParentFolder, "*.schema.mof");
177+
178+
// Ensure Schema file exists and is the only one in the DSCResource folder
179+
if (schemaMofFile != null && schemaMofFile.Count() == 1)
180+
{
181+
// Run DSC Rules only on module that matches the schema.mof file name without extension
182+
if (String.Equals(schemaMofFile[0].Replace("schema.mof", "psm1"), filePath, StringComparison.OrdinalIgnoreCase))
183+
{
184+
return true;
185+
}
186+
}
187+
}
188+
}
189+
}
190+
191+
return false;
192+
}
193+
159194
/// <summary>
160195
/// Given a commandast, checks whether positional parameters are used or not.
161196
/// </summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#AvoidUsingWriteHost
2+
**Severity Level: Warning**
3+
4+
5+
##Description
6+
7+
It is generally accepted that you should never use Write-Host to create any script output whatsoever, unless your script (or function, or whatever) uses the Show verb (as in, Show-Performance). That verb explicitly means “show on the screen, with no other possibilities.” Like Show-Command.
8+
9+
##How to Fix
10+
11+
PTo fix a violation of this rule, please replace Write-Host with Write-Output.
12+
13+
##Example
14+
15+
Wrong:
16+
17+
```
18+
function Test
19+
{
20+
...
21+
Write-Host "Executing.."
22+
}
23+
```
24+
25+
Correct:
26+
27+
```
28+
function Test
29+
{
30+
...
31+
Write-Output "Executing.."
32+
}
33+
34+
function Show-Something
35+
{
36+
Write-Host "show something on screen";
37+
}
38+
```

Rules/AvoidUninitializedVariable.cs

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

1313
using System;
14+
using System.Linq;
1415
using System.Collections.Generic;
1516
using System.Management.Automation.Language;
1617
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
@@ -51,15 +52,31 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5152

5253
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst || item is FunctionMemberAst, true);
5354

54-
foreach (var funcAst in funcAsts)
55+
// Checks whether this is a dsc resource file (we don't raise this rule for get, set and test-target resource
56+
bool isDscResourceFile = Helper.Instance.IsDscResourceModule(fileName);
57+
58+
List<string> targetResourcesFunctions = new List<string>( new string[] { "get-targetresource", "set-targetresource", "test-targetresource" });
59+
60+
foreach (FunctionDefinitionAst funcAst in funcAsts)
5561
{
5662
// Finds all VariableExpressionAst.
5763
IEnumerable<Ast> varAsts = funcAst.FindAll(testAst => testAst is VariableExpressionAst, true);
5864

65+
HashSet<string> paramVariables = new HashSet<string>();
66+
67+
if (isDscResourceFile && targetResourcesFunctions.Contains(funcAst.Name, StringComparer.OrdinalIgnoreCase))
68+
{
69+
// don't raise the rules for variables in the param block.
70+
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
71+
{
72+
paramVariables.UnionWith(funcAst.Body.ParamBlock.Parameters.Select(paramAst => paramAst.Name.VariablePath.UserPath));
73+
}
74+
}
75+
5976
// Iterates all VariableExpressionAst and check the command name.
6077
foreach (VariableExpressionAst varAst in varAsts)
6178
{
62-
if (Helper.Instance.IsUninitialized(varAst, funcAst))
79+
if (Helper.Instance.IsUninitialized(varAst, funcAst) && !paramVariables.Contains(varAst.VariablePath.UserPath))
6380
{
6481
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUninitializedVariableError, varAst.VariablePath.UserPath),
6582
varAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, varAst.VariablePath.UserPath);

Rules/ProvideVerboseMessage.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Linq;
1516
using System.Management.Automation.Language;
1617
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1718
using System.ComponentModel.Composition;
1819
using System.Globalization;
20+
using System.Management.Automation;
1921

2022
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2123
{
@@ -57,6 +59,15 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
5759
return AstVisitAction.SkipChildren;
5860
}
5961

62+
//Write-Verbose is not required for non-advanced functions
63+
if (funcAst.Body == null || funcAst.Body.ParamBlock == null
64+
|| funcAst.Body.ParamBlock.Attributes == null ||
65+
funcAst.Body.ParamBlock.Parameters == null ||
66+
!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
67+
{
68+
return AstVisitAction.Continue;
69+
}
70+
6071
var commandAsts = funcAst.Body.FindAll(testAst => testAst is CommandAst, false);
6172
bool hasVerbose = false;
6273

Tests/Rules/AvoidGlobalOrUnitializedVars.tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ $nonInitializedName = "PSAvoidUninitializedVariable"
55
$nonInitializedMessage = "Variable 'a' is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
66
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
77
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalOrUnitializedVars.ps1
8+
$dscResourceViolations = Invoke-ScriptAnalyzer $directory\DSCResources\MSFT_WaitForAny\MSFT_WaitForAny.psm1 | Where-Object {$_.RuleName -eq $nonInitializedName}
89
$globalViolations = $violations | Where-Object {$_.RuleName -eq $globalName}
910
$nonInitializedViolations = $violations | Where-Object {$_.RuleName -eq $nonInitializedName}
1011
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidGlobalOrUnitializedVarsNoViolations.ps1
@@ -17,6 +18,10 @@ Describe "AvoidGlobalVars" {
1718
$globalViolations.Count | Should Be 1
1819
}
1920

21+
It "has 4 violations for dsc resources (not counting the variables in parameters)" {
22+
$dscResourceViolations.Count | Should Be 4
23+
}
24+
2025
It "has the correct description message" {
2126
$violations[0].Message | Should Match $globalMessage
2227
}

Tests/Rules/GoodCmdlet.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ function Get-File
9292
if ($pscmdlet.ShouldContinue("Yes", "No")) {
9393
}
9494
}
95+
}
96+
97+
#Write-Verbose should not be required because this is not an advanced function
98+
function Get-SimpleFunc
99+
{
100+
95101
}

0 commit comments

Comments
 (0)