Skip to content

Commit 1778d2a

Browse files
committed
Merge pull request #148 from PowerShell/severityIgnoreCase
Severity ignore case
2 parents 5d1dfa5 + c8a5395 commit 1778d2a

File tree

6 files changed

+356
-5
lines changed

6 files changed

+356
-5
lines changed

.gitignore

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.suo
6+
*.user
7+
*.userosscache
8+
*.sln.docstates
9+
10+
# User-specific files (MonoDevelop/Xamarin Studio)
11+
*.userprefs
12+
13+
# Build results
14+
[Dd]ebug/
15+
[Dd]ebugPublic/
16+
[Rr]elease/
17+
[Rr]eleases/
18+
x64/
19+
x86/
20+
build/
21+
bld/
22+
[Bb]in/
23+
[Oo]bj/
24+
25+
# Visual Studo 2015 cache/options directory
26+
.vs/
27+
28+
# MSTest test Results
29+
[Tt]est[Rr]esult*/
30+
[Bb]uild[Ll]og.*
31+
32+
# NUNIT
33+
*.VisualState.xml
34+
TestResult.xml
35+
36+
# Build Results of an ATL Project
37+
[Dd]ebugPS/
38+
[Rr]eleasePS/
39+
dlldata.c
40+
41+
# DNX
42+
project.lock.json
43+
artifacts/
44+
45+
*_i.c
46+
*_p.c
47+
*_i.h
48+
*.ilk
49+
*.meta
50+
*.obj
51+
*.pch
52+
*.pdb
53+
*.pgc
54+
*.pgd
55+
*.rsp
56+
*.sbr
57+
*.tlb
58+
*.tli
59+
*.tlh
60+
*.tmp
61+
*.tmp_proj
62+
*.log
63+
*.vspscc
64+
*.vssscc
65+
.builds
66+
*.pidb
67+
*.svclog
68+
*.scc
69+
70+
# Chutzpah Test files
71+
_Chutzpah*
72+
73+
# Visual C++ cache files
74+
ipch/
75+
*.aps
76+
*.ncb
77+
*.opensdf
78+
*.sdf
79+
*.cachefile
80+
81+
# Visual Studio profiler
82+
*.psess
83+
*.vsp
84+
*.vspx
85+
86+
# TFS 2012 Local Workspace
87+
$tf/
88+
89+
# Guidance Automation Toolkit
90+
*.gpState
91+
92+
# ReSharper is a .NET coding add-in
93+
_ReSharper*/
94+
*.[Rr]e[Ss]harper
95+
*.DotSettings.user
96+
97+
# JustCode is a .NET coding addin-in
98+
.JustCode
99+
100+
# TeamCity is a build add-in
101+
_TeamCity*
102+
103+
# DotCover is a Code Coverage Tool
104+
*.dotCover
105+
106+
# NCrunch
107+
_NCrunch_*
108+
.*crunch*.local.xml
109+
110+
# MightyMoose
111+
*.mm.*
112+
AutoTest.Net/
113+
114+
# Web workbench (sass)
115+
.sass-cache/
116+
117+
# Installshield output folder
118+
[Ee]xpress/
119+
120+
# DocProject is a documentation generator add-in
121+
DocProject/buildhelp/
122+
DocProject/Help/*.HxT
123+
DocProject/Help/*.HxC
124+
DocProject/Help/*.hhc
125+
DocProject/Help/*.hhk
126+
DocProject/Help/*.hhp
127+
DocProject/Help/Html2
128+
DocProject/Help/html
129+
130+
# Click-Once directory
131+
publish/
132+
133+
# Publish Web Output
134+
*.[Pp]ublish.xml
135+
*.azurePubxml
136+
# TODO: Comment the next line if you want to checkin your web deploy settings
137+
# but database connection strings (with potential passwords) will be unencrypted
138+
*.pubxml
139+
*.publishproj
140+
141+
# NuGet Packages
142+
*.nupkg
143+
# The packages folder can be ignored because of Package Restore
144+
**/packages/*
145+
# except build/, which is used as an MSBuild target.
146+
!**/packages/build/
147+
# Uncomment if necessary however generally it will be regenerated when needed
148+
#!**/packages/repositories.config
149+
150+
# Windows Azure Build Output
151+
csx/
152+
*.build.csdef
153+
154+
# Windows Store app package directory
155+
AppPackages/
156+
157+
# Visual Studio cache files
158+
# files ending in .cache can be ignored
159+
*.[Cc]ache
160+
# but keep track of directories ending in .cache
161+
!*.[Cc]ache/
162+
163+
# Others
164+
ClientBin/
165+
[Ss]tyle[Cc]op.*
166+
~$*
167+
*~
168+
*.dbmdl
169+
*.dbproj.schemaview
170+
*.pfx
171+
*.publishsettings
172+
node_modules/
173+
bower_components/
174+
175+
# RIA/Silverlight projects
176+
Generated_Code/
177+
178+
# Backup & report files from converting an old project file
179+
# to a newer Visual Studio version. Backup files are not needed,
180+
# because we have git ;-)
181+
_UpgradeReport_Files/
182+
Backup*/
183+
UpgradeLog*.XML
184+
UpgradeLog*.htm
185+
186+
# SQL Server files
187+
*.mdf
188+
*.ldf
189+
190+
# Business Intelligence projects
191+
*.rdl.data
192+
*.bim.layout
193+
*.bim_*.settings
194+
195+
# Microsoft Fakes
196+
FakesAssemblies/
197+
198+
# Node.js Tools for Visual Studio
199+
.ntvs_analysis.dat
200+
201+
# Visual Studio 6 build log
202+
*.plg
203+
204+
# Visual Studio 6 workspace options file
205+
*.opt
206+
207+
##Our project binplace location
208+
PSScriptAnalyzer/

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected override void ProcessRecord()
142142
{
143143
if (severity != null)
144144
{
145-
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item));
145+
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item, true));
146146
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
147147
}
148148

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ private void AnalyzeFile(string filePath)
581581

582582
if (severity != null)
583583
{
584-
var diagSeverity = severity.Select(item => Enum.Parse(typeof(DiagnosticSeverity), item));
584+
var diagSeverity = severity.Select(item => Enum.Parse(typeof(DiagnosticSeverity), item, true));
585585
diagnostics = diagnostics.Where(item => diagSeverity.Contains(item.Severity)).ToList();
586586
}
587587

PowerShellBestPractices.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#PowerShell Best Practices
2+
3+
The following guidelines come from a combined effort from both the PowerShell team and the community. We will use this guideline to define rules for PSScriptAnalyzer. Please feel free to propose additional guidelines and rules for PSScriptAnalyzer.
4+
**Note: The hyperlink next to each guidelines will redirect to documentation page for the rule that is already implemented.
5+
6+
##Cmdlet Design Rules
7+
###Severity: Error
8+
###Severity: Warning
9+
- Use Only Approved Verbs [UseApprovedVerbs](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseApprovedVerbs.md)
10+
- Cmdlets Names: Characters that cannot be Used [AvoidReservedCharInCmdlet](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidReservedCharInCmdlet.md)
11+
- Parameter Names that cannot be Used [AvoidReservedParams](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidReservedParams.md)
12+
- Support Confirmation Requests [UseShouldProcessCorrectly](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseShouldProcessCorrectly.md) and [UseShouldProcessForStateChangingFunctions](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseShouldProcessForStateChangingFunctions.md)
13+
- Nouns should be singular [UseSingularNouns](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseSingularNouns.md)
14+
- Module Manifest Fields [MissingModuleManifestField](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/MissingModuleManifestField.md)
15+
- Version
16+
- Author
17+
- Description
18+
- LicenseUri (for PowerShell Gallery)
19+
- Must call ShouldProcess when ShouldProcess attribute is present and vice versa.[UseShouldProcessCorrectly](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseShouldProcessCorrectly.md)
20+
- Switch parameters should not default to true  [AvoidDefaultTrueValueSwtichParameter](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidDefaultTrueValueSwitchParameter.md)
21+
22+
###Severity: Information
23+
24+
###Severity: TBD
25+
- Support Force Parameter for Interactive Session
26+
- If your cmdlet is used interactively, always provide a Force parameter to override the interactive actions, such as prompts or reading lines of input). This is important because it allows your cmdlet to be used in non-interactive scripts and hosts. The following methods can be implemented by an interactive host.
27+
- Document Output Objects
28+
- Module must be loadable
29+
- No syntax errors
30+
- Unresolved dependencies are an error
31+
- Derive from the Cmdlet or PSCmdlet Classes
32+
- Specify the Cmdlet Attribute
33+
- Override an Input Processing Method
34+
- Specify the OutputType Attribute
35+
- Write Single Records to the Pipeline
36+
- Make Cmdlets Case-Insensitive and Case-Preserving
37+
38+
##Script Functions
39+
###Severity: Error
40+
41+
###Severity: Warning
42+
- Avoid using alias [AvoidAlias](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidAlias.md)
43+
- Avoid using deprecated WMI cmdlets [AvoidUsingWMICmdlet](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingWMICmdlet.md)
44+
- Empty catch block should not be used [AvoidEmptyCatchBlock](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidEmptyCatchBlock.md)
45+
- Invoke existing cmdlet with correct parameters [UseCmdletCorrectly](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseCmdletCorrectly.md)
46+
- Cmdlets should have ShouldProcess/ShouldContinue and Force param if certain system-modding verbs are present (Update, Set, Remove, New)[UseShouldProcessForStateChangingFunctions](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseShouldProcessForStateChangingFunctions.md)
47+
- Positional parameters should be avoided [AvoidUsingPositionalParameters](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingPositionalParameters.md)
48+
- Non-global variables must be initialized. Those that are supposed to be global and not initialized must have “global:” (includes for loop initializations)[AvoidUninitializedVariable](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUninitializedVariable.md)
49+
- Global variables should be avoided. [AvoidGlobalVars](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidGlobalVars.md)
50+
- Declared variables must be used in more than just their assignment. [UseDeclaredVarsMoreThanAssignments](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseDeclaredVarsMoreThanAssignments.md)
51+
- No trap statments should be used [AvoidTrapStatement](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidTrapStatement.md)
52+
- No Invoke-Expression [AvoidUsingInvokeExpression](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingInvokeExpression.md)
53+
54+
###Severity: Information
55+
56+
###Severity: TBD
57+
- Clear-Host should not be used
58+
- File paths should not be used (UNC)
59+
- Error Handling
60+
- Use -ErrorAction Stop when calling cmdlets
61+
- Use $ErrorActionPreference = 'Stop'/' Continue' when calling non-cmdlets
62+
- Avoid using flags to handle errors
63+
- Avoid using $?
64+
- Avoid testing for a null variable as an error condition
65+
- Copy $Error[0] to your own variable
66+
- Avoid using pipelines in scripts
67+
- If a return type is declared, the cmdlet must return that type. If a type is returned, a return type must be declared.
68+
69+
70+
71+
##Scripting Style
72+
###Severity: Error
73+
74+
###Severity: Warning
75+
- Don't use write-host unless writing to the host is all you want to do [AvoidUsingWriteHost](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingWriteHost.md)
76+
77+
###Severity: Information
78+
- Write comment-based help [ProvideCommentHelp](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/ProvideCommentHelp.md)
79+
- Use write-verbose to give information to someone running your script [ProvideVerboseMessage](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/ProvideVerboseMessage.md)
80+
###Severity: TBD
81+
- Provide usage Examples
82+
- Use the Notes section for detail on how the tool work
83+
- Should have help on every exported command (including parameter documentation
84+
- Document the version of PowerShell that script was written for
85+
- Indent your code
86+
- Avoid backticks
87+
88+
89+
##Script Security
90+
###Severity: Error
91+
- Password should be secure string [AvoidUsingPlainTextForPassword](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingPlainTextForPassword.md)- Should never have both -Username and -Password parameters (should take credentials)[UsePSCredentialType](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UsePSCredentialType.md)
92+
- -ComputerName hardcoded should not be used (information disclosure)[AvoidUsingComputerNameHardcoded](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingComputerNameHardcoded.md)
93+
- ConvertTo-SecureString with plaintext should not be used (information disclosure) [AvoidUsingConvertToSecureStringWithPlainText](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingConvertToSecureStringWithPlainText.md)
94+
95+
###Severity: Warning
96+
- Password = 'string' should not be used. (information disclosure) [AvoidUsingUsernameAndPasswordParams](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingUsernameAndPasswordParams.md)
97+
- Internal URLs should not be used (information disclosure)[AvoidUsingFilePath](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingFilePath.md)
98+
99+
###Severity: Information
100+
101+
###Severity: TBD
102+
- APIKey and Credentials variables that are initialized (information disclosure)
103+
104+
105+
##DSC Related Rules
106+
###Severity: Error
107+
- Use standard DSC methods [UseStandardDSCFunctionsInResource](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseStandardDSC FunctionsInResource.md)
108+
- Use identical mandatory parameters for all DSC methods [UseIdenticalMandatoryParametersDSC](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseIdenticalMandatoryParametersDSC.md)
109+
- Use identical parameters for Set and Test DSC methods [UseIdenticalParametersDSC](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/UseIdenticalParametersDSC.md)
110+
111+
###Severity: Warning
112+
113+
###Severity: Information
114+
- All of the following three rule are grouped by: [ReturnCorrectTypeDSCFunctions](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/ReturnCorrectTypeDSCFunctions.md)
115+
- Avoid return any object from a Set-TargetResource function
116+
- Returning a Boolean object from a Test-TargetResource function
117+
- Returning an object from a Get-TargetResource function
118+
- DSC resources should have DSC tests [DSCTestsPresent](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/DscTestsPresent.md)
119+
- DSC resources should have DSC examples [DSCExamplesPresent](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/DscExamplesPresent.md)
120+
121+
###Severity: TBD
122+
- For PowerShell V4: Resource module contains .psd1 file and schema.mof for every resource
123+
- MOF has description for each element [IssueOpened](https://github.com/PowerShell/PSScriptAnalyzer/issues/131)
124+
- Resource module must alwasy contain .psd1 file and schema.mof (for non-class resource).
125+
- Use ShouldProcess for a Set DSC method
126+
- Resource module contains Resources folder which contains the resources [IssueOpened](https://github.com/PowerShell/PSScriptAnalyzer/issues/130)
127+
128+
129+
130+
###Reference:
131+
Cmdlet Development Guidelines from MSDN site (Cmdlet Development Guidelines)
132+
133+
The Community Book of PowerShell Practices (Compiled by Don Jones and Matt Penny and the Windows PowerShell Community)

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ Describe "Test RuleExtension" {
113113
Describe "TestSeverity" {
114114
It "filters rules based on the specified rule severity" {
115115
$rules = Get-ScriptAnalyzerRule -Severity Error
116-
$rules.Count | Should be 4
116+
$rules.Count | Should be 6
117117
}
118118

119119
It "filters rules based on multiple severity inputs"{
120120
$rules = Get-ScriptAnalyzerRule -Severity Error,Information
121-
$rules.Count | Should be 8
121+
$rules.Count | Should be 9
122+
}
123+
124+
It "takes lower case inputs" {
125+
$rules = Get-ScriptAnalyzerRule -Severity error
126+
$rules.Count | Should be 6
122127
}
123128
}
124129

@@ -130,6 +135,6 @@ Describe "TestWildCard" {
130135

131136
It "filters rules based on wild card input and severity"{
132137
$rules = Get-ScriptAnalyzerRule -Name PSDSC* -Severity Information
133-
$rules.Count | Should be 2
138+
$rules.Count | Should be 1
134139
}
135140
}

0 commit comments

Comments
 (0)