Skip to content

Adding DscTestsPresent rule #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 82 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
b4338b0
Update README.md
yutingc Apr 23, 2015
4cd6e49
Update README.md
yutingc Apr 23, 2015
a01b048
Change property display name to be the same as property name
yutingc Apr 23, 2015
ad6350d
Merge pull request #63 from PowerShell/propertyName
yutingc Apr 24, 2015
7492146
Merge pull request #64 from PowerShell/BugFixes
raghushantha Apr 24, 2015
f58b64b
Merge pull request #65 from PowerShell/development
raghushantha Apr 24, 2015
0245e93
Change severity column in scriptanalyzer.format
Apr 24, 2015
25a974b
Added Author field in the Manifest.
raghushantha Apr 24, 2015
90a3b5a
Add wild card support to Get-ScriptAnalyzerRule
yutingc Apr 25, 2015
b43039e
Updated test as well
yutingc Apr 25, 2015
6bac47e
Merge pull request #70 from PowerShell/GetRuleWildCard
yutingc Apr 27, 2015
395dc2a
Fix typo in file name for AvoidUninitializedVariable.cs
raghushantha Apr 27, 2015
9bfb659
Update AvoidUninitializedVariable.md
yutingc Apr 27, 2015
7033a75
Merge pull request #75 from PowerShell/AvoidUninitializedVariableFixB…
raghushantha Apr 27, 2015
f5b8031
AvoidUninitialized Variable Rule must recognize built-in variable Sys…
raghushantha Apr 27, 2015
2d95ca6
Change parameter -RuleName to -Name
yutingc Apr 27, 2015
42b677d
Update the parameter in the tests as well
yutingc Apr 27, 2015
e2e34b5
Merge pull request #77 from PowerShell/ExecutionContextUninitializedV…
raghushantha Apr 27, 2015
58f5fbe
Updating installation instructions
KarolKaczmarek Apr 28, 2015
1d2bfcc
Rename PSScriptAnalyzer.psd1 to ScriptAnalyzer.psd1
yutingc Apr 28, 2015
7c969c1
Merge pull request #79 from KarolKaczmarek/installation_instructions
yutingc Apr 28, 2015
b9d2792
Update README.md
yutingc Apr 28, 2015
8ebe5ec
Rename solution file
yutingc Apr 28, 2015
ea65794
Merge branch 'BugFixes'
yutingc Apr 28, 2015
420f995
Merge branch 'master' of https://github.com/PowerShell/PSScriptAnalyzer
yutingc Apr 28, 2015
06544ec
Update README.md
yutingc Apr 28, 2015
8c7b4d5
Rename ScriptAnalyzer.psd1 to PSScriptAnalyzer.psd1
yutingc Apr 28, 2015
c03f47c
Updated Author, CompanyName and Copyright properties
raghushantha May 4, 2015
e2650ba
Merge pull request #88 from PowerShell/BugFixes
raghushantha May 4, 2015
8919af7
Fix exceptions in importing CustomizedRule
yutingc May 4, 2015
ff75584
Revert "Fix exceptions in importing CustomizedRule"
yutingc May 4, 2015
53ca175
Fix bugs in importing external rule
yutingc May 4, 2015
c5f1ecc
Updated string comparison
yutingc May 4, 2015
89b4e23
Modified string comparison options
yutingc May 4, 2015
763d990
add waffle.io badge
waffle-iron May 4, 2015
0f4f8cb
Merge pull request #91 from waffle-iron/master
raghushantha May 4, 2015
129127b
Added new section about Project Management
raghushantha May 4, 2015
be781aa
Added unit test
yutingc May 4, 2015
b73aa4d
Added unit tests
yutingc May 5, 2015
cdcc4e5
Added changes in ExternalRule
yutingc May 5, 2015
5eff3c4
Merge pull request #90 from PowerShell/customizedRule
yutingc May 5, 2015
49c0e1e
Suppress write host for function that starts with show
May 5, 2015
5b0785b
Merge pull request #93 from PowerShell/writehostbug
May 5, 2015
0c82a4a
Add documentation for customized rules
yutingc May 5, 2015
296f091
Change code formatting
yutingc May 5, 2015
d2057f1
Modified some wording
yutingc May 5, 2015
0d37d1f
Update the list order
yutingc May 5, 2015
4267cae
Ordered list
yutingc May 5, 2015
b6c17b3
Update CustomizedRuleDocumentation.md
yutingc May 5, 2015
11ced68
Update CustomizedRuleDocumentation.md
yutingc May 5, 2015
b98774c
Update README.md
raghushantha May 5, 2015
6cb8120
Updated existing rule for verifiying the presence of WMI cmdlets in a…
raghushantha May 5, 2015
5e2a145
Updated existing rule for verifiying the presence of WMI cmdlets in a…
raghushantha May 5, 2015
0d539ee
Adding scaffolding for DscTestsPresent rule
KarolKaczmarek May 5, 2015
5ca074b
Adding implementation for non-class based resources
KarolKaczmarek May 5, 2015
d099687
Adding rule to project file
KarolKaczmarek May 5, 2015
ed2f291
Adding implementation for class based resources
KarolKaczmarek May 5, 2015
bf204bd
Adding tests
KarolKaczmarek May 5, 2015
dcb5d6e
Tests for [AvoidUsingWMICmdlet Rule
raghushantha May 5, 2015
f00cfa6
Merge pull request #98 from PowerShell/AvoidUsingWMICmdletBranch
raghushantha May 5, 2015
4238f90
Do not require Write-Verbose in scripts or functions without the Cmdl…
GoodOlClint May 6, 2015
958e65d
Merge pull request #100 from GoodOlClint/BugFixes
yutingc May 6, 2015
4f8e7a6
Revert "Only Trigger PSProvideVerboseMessage in Advanced Scripts or F…
yutingc May 6, 2015
c488f10
Merge pull request #101 from PowerShell/revert-100-BugFixes
yutingc May 6, 2015
3aa8ee8
Revert "Revert "Only Trigger PSProvideVerboseMessage in Advanced Scri…
yutingc May 6, 2015
974a255
Merge pull request #102 from PowerShell/revert-101-revert-100-BugFixes
yutingc May 6, 2015
f3b55e6
Rule Documentation for WMI Cmdlet
raghushantha May 6, 2015
72858d2
Update AvoidUsingWMICmdlet.md
raghushantha May 6, 2015
6f2a57b
Update AvoidUsingWMICmdlet.md
raghushantha May 6, 2015
5e7d2f6
Update AvoidUsingWMICmdlet.md
yutingc May 6, 2015
350816b
Merge pull request #105 from PowerShell/AvoidWMICmdletRuleDocumentati…
raghushantha May 6, 2015
8730151
Create documentation for DscTestsPresent rule
KarolKaczmarek May 6, 2015
a29203d
Revert "Revert "Revert "Only Trigger PSProvideVerboseMessage in Advan…
yutingc May 6, 2015
f12e076
Merge pull request #107 from PowerShell/revert-102-revert-101-revert-…
yutingc May 6, 2015
ec4afc6
Merge pull request #108 from PowerShell/BugFixes
raghushantha May 6, 2015
91fe9d0
Merge pull request #94 from PowerShell/customizedRuleDocumentati
yutingc May 6, 2015
8a6e394
Fixing tests
KarolKaczmarek May 6, 2015
2a986e0
Merge branch 'DscTestsPresentRule' of github.com:KarolKaczmarek/PSScr…
KarolKaczmarek May 6, 2015
e99e88d
Regenerated strings.designer since the existing rule name (WMI) was c…
raghushantha May 6, 2015
57a7719
Merge pull request #109 from PowerShell/BugFixes
raghushantha May 6, 2015
4fbd842
Merge branch 'master' of https://github.com/PowerShell/PSScriptAnalyz…
KarolKaczmarek May 6, 2015
5892a67
Resolving conflicts
KarolKaczmarek May 6, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions CustomizedRuleDocumentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
##Documentation for Customized Rules in PowerShell Scripts
PSScriptAnalyzer uses MEF(Managed Extensibility Framework) to import all rules defined in the assembly. It can also consume rules written in PowerShell scripts. When calling Invoke-ScriptAnalyzer, users can pass customized rules using parameter -CustomizedRulePath to apply rule checkings on the scripts.

This documentation serves as a basic guideline on how to define customized rules.

###Basics
- Functions should have comment-based help. Make sure .DESCRIPTION field is there, as it will be consumed as rule description for the customized rule.
```
<#
.SYNOPSIS
Name of your rule.
.DESCRIPTION
This would be the description of your rule. Please refer to Rule Documentation for consistent rule messages.
.EXAMPLE
.INPUTS
.OUTPUTS
.NOTES
#>
```

- Output type should be DiagnosticRecord:
```
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
```

- Make sure each function takes either a Token or an Ast as a parameter
```
Param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$testAst
)
```

- DiagnosticRecord should have four properties: Message, Extent, RuleName and Severity
```
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "This is a sample rule";
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Warning"}
```

- Make sure you export the function(s) at the end of the script using Export-ModuleMember
```
Export-ModuleMember -Function (FunctionName)
```

###Example
```
<#
.SYNOPSIS
Uses #Requires -RunAsAdministrator instead of your own methods.
.DESCRIPTION
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
Script developers does not need to write their own methods any more.
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods.
.EXAMPLE
Measure-RequiresRunAsAdministrator -ScriptBlockAst $ScriptBlockAst
.INPUTS
[System.Management.Automation.Language.ScriptBlockAst]
.OUTPUTS
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]
.NOTES
None
#>
function Measure-RequiresRunAsAdministrator
{
[CmdletBinding()]
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
Param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$ScriptBlockAst
)

Process
{
$results = @()
try
{
#region Define predicates to find ASTs.
# Finds specific method, IsInRole.
[ScriptBlock]$predicate1 = {
param ([System.Management.Automation.Language.Ast]$Ast)
[bool]$returnValue = $false
if ($Ast -is [System.Management.Automation.Language.MemberExpressionAst])
{
[System.Management.Automation.Language.MemberExpressionAst]$meAst = $ast;
if ($meAst.Member -is [System.Management.Automation.Language.StringConstantExpressionAst])
{
[System.Management.Automation.Language.StringConstantExpressionAst]$sceAst = $meAst.Member;
if ($sceAst.Value -eq "isinrole")
{
$returnValue = $true;
}
}
}
return $returnValue
}

# Finds specific value, [system.security.principal.windowsbuiltinrole]::administrator.
[ScriptBlock]$predicate2 = {
param ([System.Management.Automation.Language.Ast]$Ast)
[bool]$returnValue = $false
if ($ast -is [System.Management.Automation.Language.AssignmentStatementAst])
{
[System.Management.Automation.Language.AssignmentStatementAst]$asAst = $Ast;
if ($asAst.Right.ToString().ToLower() -eq "[system.security.principal.windowsbuiltinrole]::administrator")
{
$returnValue = $true
}
}
return $returnValue
}
#endregion
#region Finds ASTs that match the predicates.

[System.Management.Automation.Language.Ast[]]$methodAst = $ScriptBlockAst.FindAll($predicate1, $true)
[System.Management.Automation.Language.Ast[]]$assignmentAst = $ScriptBlockAst.FindAll($predicate2, $true)
if ($null -ne $ScriptBlockAst.ScriptRequirements)
{
if ((!$ScriptBlockAst.ScriptRequirements.IsElevationRequired) -and
($methodAst.Count -ne 0) -and ($assignmentAst.Count -ne 0))
{
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
"Extent" = $assignmentAst.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Information"}
$results += $result
}
}
else
{
if (($methodAst.Count -ne 0) -and ($assignmentAst.Count -ne 0))
{
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
"Extent" = $assignmentAst.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Information"}
$results += $result
}
}
return $results
#endregion
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}
```
More examples can be found in *Tests\Engine\CommunityRules*
10 changes: 8 additions & 2 deletions Engine/Generic/ExternalRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class ExternalRule : IExternalRule
string param = string.Empty;
string srcName = string.Empty;
string modPath = string.Empty;

string paramType = string.Empty;

public string GetName()
{
Expand All @@ -55,6 +55,11 @@ public SourceType GetSourceType()
return SourceType.Module;
}

public string GetParameterType()
{
return this.paramType;
}

//Set the community rule level as warning as the current implementation does not require user to specify rule severity when defining their functions in PS scripts
public RuleSeverity GetSeverity()
{
Expand All @@ -80,14 +85,15 @@ public ExternalRule()

}

public ExternalRule(string name, string commonName, string desc, string param, string srcName, string modPath)
public ExternalRule(string name, string commonName, string desc, string param, string paramType, string srcName, string modPath)
{
this.name = name;
this.commonName = commonName;
this.desc = desc;
this.param = param;
this.srcName = srcName;
this.modPath = modPath;
this.paramType = paramType;
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions Engine/Generic/RuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RuleInfo
/// Name: The name of the rule.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public string Name
public string RuleName
{
get { return name; }
private set { name = value; }
Expand Down Expand Up @@ -102,7 +102,7 @@ public RuleSeverity Severity
/// <param name="sourceName">Source name of the rule.</param>
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity)
{
Name = name;
RuleName = name;
CommonName = commonName;
Description = description;
SourceType = sourceType;
Expand Down
7 changes: 5 additions & 2 deletions Engine/PSScriptAnalyzer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#

@{

# Author of this module
Author = 'Microsoft Corporation'

# Script module or binary module file associated with this manifest.
RootModule = 'Microsoft.Windows.Powershell.ScriptAnalyzer.dll'
Expand All @@ -14,10 +17,10 @@ ModuleVersion = '1.0'
GUID = '324fc715-36bf-4aee-8e58-72e9b4a08ad9'

# Company or vendor of this module
CompanyName = 'Microsoft'
CompanyName = 'Microsoft Corporation'

# Copyright statement for this module
Copyright = '(c) 2015. All rights reserved.'
Copyright = '(c) Microsoft Corporation 2015. All rights reserved.'

# Description of the functionality provided by this module
Description = 'PSScriptAnalyzer provides script analysis and checks for potential code defects in the scripts by applying a group of builtin or customized rules on the scripts being analyzed.'
Expand Down
Loading