Skip to content

Commit 12f937a

Browse files
committed
Merge pull request #136 from PowerShell/BestPractice
New Best practice formatting
2 parents c1d558d + 4e1a573 commit 12f937a

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

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)

0 commit comments

Comments
 (0)