Skip to content

Commit 3f9ef44

Browse files
committed
Add rule documentations for all PS rules
1 parent 561f108 commit 3f9ef44

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#UseDeclaredVarsMoreThanAssignments
2+
**Severity Level: Info**
3+
4+
5+
##Description
6+
7+
Checks that variables are used in more than just their assignment. Generally this is a red flag that a variable is not needed. This rule does not check if the assignment and usage are in the same function.
8+
9+
10+
##How to Fix
11+
12+
Please consider remove the variables that are declared but not used outside of the function.
13+
14+
15+
##Example
16+
Wrong:
17+
18+
function Test
19+
{
20+
$declaredVar = "Declared"
21+
$declaredVar2 = "Not used"
22+
}
23+
24+
Correct:
25+
26+
function Test
27+
{
28+
$declaredVar = "Declared just for fun"
29+
Write-Output $declaredVar
30+
}
31+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#UsePSCredentialType
2+
**Severity Level: Warning**
3+
4+
5+
##Description
6+
7+
Checks that cmdlets that have a Credential parameter accept PSCredential. This comes from the PowerShell teams best practices.
8+
9+
##How to Fix
10+
11+
Please change the parameter type to be PSCredential.
12+
13+
##Example
14+
15+
Wrong:
16+
17+
function Credential([string]$credential)
18+
{
19+
...
20+
}
21+
22+
Correct:
23+
24+
function Credential([PSCredential]$credential)
25+
{
26+
...
27+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#UseShouldProcessCorrectly
2+
**Severity Level: Error**
3+
4+
5+
##Description
6+
7+
Checks that if the SupportsShouldProcess is present, the function calls ShouldProcess/ShouldContinue and vice versa. Scripts with one or the other but not both will generally run into an error or unexpected behavior.
8+
9+
10+
##How to Fix
11+
12+
To fix a violation of this rule, please call ShouldProcess method in advanced functions when SupportsShouldProcess argument is present. Or please add SupportsShouldProcess argument when calling ShouldProcess.You can get more details by running “Get-Help about_Functions_CmdletBindingAttribute” and “Get-Help about_Functions_Advanced_Methods” command in Windows PowerShell.
13+
14+
##Example
15+
16+
Wrong:
17+
18+
function Verb-Files
19+
{
20+
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
21+
SupportsShouldProcess=$true,
22+
PositionalBinding=$false,
23+
HelpUri = 'http://www.microsoft.com/',
24+
ConfirmImpact='Medium')]
25+
[Alias()]
26+
[OutputType([String])]
27+
Param
28+
(
29+
# Param1 help description
30+
[Parameter(Mandatory=$true,
31+
ValueFromPipeline=$true,
32+
ValueFromPipelineByPropertyName=$true,
33+
ValueFromRemainingArguments=$false,
34+
Position=0,
35+
ParameterSetName='Parameter Set 1')]
36+
[ValidateNotNull()]
37+
[ValidateNotNullOrEmpty()]
38+
[ValidateCount(0,5)]
39+
[ValidateSet("sun", "moon", "earth")]
40+
[Alias("p1")]
41+
$Param1,
42+
# Param2 help description
43+
[Parameter(ParameterSetName='Parameter Set 1')]
44+
[AllowNull()]
45+
[AllowEmptyCollection()]
46+
[AllowEmptyString()]
47+
[ValidateScript({$true})]
48+
[ValidateRange(0,5)]
49+
[int]
50+
$Verbose,
51+
)
52+
53+
Begin
54+
{
55+
}
56+
Process
57+
{
58+
}
59+
End
60+
{
61+
}
62+
}
63+
64+
Correct:
65+
66+
function Get-File
67+
{
68+
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
69+
SupportsShouldProcess=$true,
70+
PositionalBinding=$false,
71+
HelpUri = 'http://www.microsoft.com/',
72+
ConfirmImpact='Medium')]
73+
[Alias()]
74+
[OutputType([String])]
75+
Param
76+
(
77+
# Param1 help description
78+
[Parameter(Mandatory=$true,
79+
ValueFromPipeline=$true,
80+
ValueFromPipelineByPropertyName=$true,
81+
ValueFromRemainingArguments=$false,
82+
Position=0,
83+
ParameterSetName='Parameter Set 1')]
84+
[ValidateNotNull()]
85+
[ValidateNotNullOrEmpty()]
86+
[ValidateCount(0,5)]
87+
[ValidateSet("sun", "moon", "earth")]
88+
[Alias("p1")]
89+
$Param1,
90+
91+
# Param2 help description
92+
[Parameter(ParameterSetName='Parameter Set 1')]
93+
[AllowNull()]
94+
[AllowEmptyCollection()]
95+
[AllowEmptyString()]
96+
[ValidateScript({$true})]
97+
[ValidateRange(0,5)]
98+
[int]
99+
$Param2,
100+
101+
# Param3 help description
102+
[Parameter(ParameterSetName='Another Parameter Set')]
103+
[ValidatePattern("[a-z]*")]
104+
[ValidateLength(0,15)]
105+
[String]
106+
$Param3,
107+
[bool]
108+
$Force
109+
)
110+
111+
Begin
112+
{
113+
}
114+
Process
115+
{
116+
if ($pscmdlet.ShouldProcess("Target", "Operation"))
117+
{
118+
Write-Verbose "Write Verbose"
119+
Get-Process
120+
}
121+
}
122+
End
123+
{
124+
if ($pscmdlet.ShouldContinue("Yes", "No")) {
125+
}
126+
}
127+
}

RuleDocumentation/UseSingularNouns.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#UseSingularNouns
2+
**Severity Level: Warning**
3+
4+
##Description
5+
6+
Cmdlet should use singular instead of plural nouns. This comes from the PowerShell teams best practices.
7+
8+
##How to Fix
9+
10+
Please correct the plural nouns to be singluar.
11+
12+
##Example
13+
14+
Wrong:
15+
16+
function Get-Files
17+
{
18+
...
19+
}
20+
21+
Correct:
22+
23+
function Get-File
24+
{
25+
...
26+
}

0 commit comments

Comments
 (0)