Skip to content

Commit 00bea7a

Browse files
authored
Update rule docs (#1711)
* Update rule docs * Revert file name changes * Feedback edits * feedback edit
1 parent a42ab5f commit 00bea7a

File tree

69 files changed

+1091
-874
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1091
-874
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ usage of Invoke-Expression etc. Additional functionalities such as exclude/inclu
5151
Usage
5252
======================
5353

54-
``` PowerShell
54+
```powershell
5555
Get-ScriptAnalyzerRule [-CustomRulePath <String[]>] [-RecurseCustomRulePath] [-Name <String[]>] [-Severity <String[]>] [<CommonParameters>]
5656
5757
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String[]>] [-RecurseCustomRulePath]
@@ -188,7 +188,7 @@ Pester-based ScriptAnalyzer Tests are located in `path/to/PSScriptAnalyzer/Tests
188188

189189
* Ensure [Pester 4.3.1](https://www.powershellgallery.com/packages/Pester/4.3.1) or higher is installed
190190
* In the root folder of your local repository, run:
191-
``` PowerShell
191+
```powershell
192192
./build -Test
193193
```
194194

@@ -251,7 +251,7 @@ Suppressing Rules
251251
You can suppress a rule by decorating a script/function or script/function parameter with .NET's [SuppressMessageAttribute](https://docs.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.suppressmessageattribute).
252252
`SuppressMessageAttribute`'s constructor takes two parameters: a category and a check ID. Set the `categoryID` parameter to the name of the rule you want to suppress and set the `checkID` parameter to a null or empty string. You can optionally add a third named parameter with a justification for suppressing the message:
253253

254-
``` PowerShell
254+
```powershell
255255
function SuppressMe()
256256
{
257257
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification='Just an example')]
@@ -265,7 +265,7 @@ function SuppressMe()
265265
All rule violations within the scope of the script/function/parameter you decorate will be suppressed.
266266

267267
To suppress a message on a specific parameter, set the `SuppressMessageAttribute`'s `CheckId` parameter to the name of the parameter:
268-
``` PowerShell
268+
```powershell
269269
function SuppressTwoVariables()
270270
{
271271
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideDefaultParameterValue', 'b')]
@@ -280,7 +280,7 @@ Use the `SuppressMessageAttribute`'s `Scope` property to limit rule suppression
280280

281281
Use the value `Function` to suppress violations on all functions within the attribute's scope. Use the value `Class` to suppress violations on all classes within the attribute's scope:
282282

283-
``` PowerShell
283+
```powershell
284284
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Scope='Function')]
285285
param(
286286
)
@@ -296,7 +296,7 @@ function InternalFunction
296296
You can further restrict suppression based on a function/parameter/class/variable/object's name by setting the `SuppressMessageAttribute's` `Target` property to a regular expression or a glob pattern. Few examples are given below.
297297

298298
Suppress `PSAvoidUsingWriteHost` rule violation in `start-bar` and `start-baz` but not in `start-foo` and `start-bam`:
299-
``` PowerShell
299+
```powershell
300300
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-ba[rz]')]
301301
param()
302302
function start-foo {
@@ -317,13 +317,13 @@ function start-bam {
317317
```
318318

319319
Suppress violations in all the functions:
320-
``` PowerShell
320+
```powershell
321321
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='*')]
322322
Param()
323323
```
324324

325325
Suppress violation in `start-bar`, `start-baz` and `start-bam` but not in `start-foo`:
326-
``` PowerShell
326+
```powershell
327327
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-b*')]
328328
Param()
329329
```
@@ -354,7 +354,7 @@ Along with `PSGallery` there are a few other built-in presets, including, `DSC`
354354
The following example excludes two rules from the default set of rules and any rule
355355
that does not output an Error or Warning diagnostic record.
356356

357-
``` PowerShell
357+
```powershell
358358
# PSScriptAnalyzerSettings.psd1
359359
@{
360360
Severity=@('Error','Warning')
@@ -365,13 +365,13 @@ that does not output an Error or Warning diagnostic record.
365365

366366
Then invoke that settings file when using `Invoke-ScriptAnalyzer`:
367367

368-
``` PowerShell
368+
```powershell
369369
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Settings PSScriptAnalyzerSettings.psd1
370370
```
371371

372372
The next example selects a few rules to execute instead of all the default rules.
373373

374-
``` PowerShell
374+
```powershell
375375
# PSScriptAnalyzerSettings.psd1
376376
@{
377377
IncludeRules=@('PSAvoidUsingPlainTextForPassword',
@@ -380,15 +380,15 @@ The next example selects a few rules to execute instead of all the default rules
380380
```
381381

382382
Then invoke that settings file:
383-
``` PowerShell
383+
```powershell
384384
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Settings PSScriptAnalyzerSettings.psd1
385385
```
386386

387387
### Implicit
388388

389389
If you place a PSScriptAnayzer settings file named `PSScriptAnalyzerSettings.psd1` in your project root, PSScriptAnalyzer will discover it if you pass the project root as the `Path` parameter.
390390

391-
```PowerShell
391+
```powershell
392392
Invoke-ScriptAnalyzer -Path "C:\path\to\project" -Recurse
393393
```
394394

RuleDocumentation/AlignAssignmentStatement.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
## Description
66

7-
Consecutive assignment statements are more readable if they are aligned. By aligned, we imply that the `equal` sign for all the assignment statements should be in the same column.
7+
Consecutive assignment statements are more readable if they are aligned. By aligned, we imply that
8+
the `equal` sign for all the assignment statements should be in the same column.
89

9-
The rule looks for key (property) value pairs in a hashtable (DSC configuration) to check if they are aligned or not. Consider the following example in which the key value pairs are not aligned.
10+
The rule looks for key (property) value pairs in a hashtable (DSC configuration) to check if they
11+
are aligned or not. Consider the following example in which the key value pairs are not aligned.
1012

1113
```powershell
1214
$hashtable = @{
@@ -24,17 +26,18 @@ $hashtable = @{
2426
}
2527
```
2628

27-
The rule will ignore hashtables in which the assignment statements are on the same line. For example, the rule will ignore `$h = {a = 1; b = 2}`.
29+
The rule ignores hashtables in which the assignment statements are on the same line. For example,
30+
the rule ignores `$h = {a = 1; b = 2}`.
2831

2932
## Configuration
3033

3134
```powershell
32-
Rules = @{
33-
PSAlignAssignmentStatement = @{
34-
Enable = $true
35-
CheckHashtable = $true
36-
}
35+
Rules = @{
36+
PSAlignAssignmentStatement = @{
37+
Enable = $true
38+
CheckHashtable = $true
3739
}
40+
}
3841
```
3942

4043
### Parameters
@@ -45,4 +48,6 @@ Enable or disable the rule during ScriptAnalyzer invocation.
4548

4649
#### CheckHashtable: bool (Default value is `$false`)
4750

48-
Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only one switch for hasthable and DSC configuration because the property value pairs in a DSC configuration are parsed as key value pairs of a hashtable.
51+
Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only
52+
one switch for hasthable and DSC configuration because the property value pairs in a DSC
53+
configuration are parsed as key-value pairs of a hashtable.

RuleDocumentation/AvoidAssignmentToAutomaticVariable.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
## Description
66

7-
`PowerShell` exposes some of its built-in variables that are known as automatic variables. Many of them are read-only and PowerShell would throw an error when trying to assign an value on those. Other automatic variables should only be assigned to in certain special cases to achieve a certain effect as a special technique.
7+
PowerShell has built-in variables known as automatic variables. Many of them are read-only and
8+
PowerShell throws an error when trying to assign an value on those. Other automatic variables should
9+
only be assigned in certain special cases to achieve a certain effect as a special technique.
810

9-
To understand more about automatic variables, see ```Get-Help about_Automatic_Variables```.
11+
To understand more about automatic variables, see `Get-Help about_Automatic_Variables`.
1012

1113
## How
1214

@@ -16,18 +18,19 @@ Use variable names in functions or their parameters that do not conflict with au
1618

1719
### Wrong
1820

19-
The variable `$Error` is an automatic variables that exists in the global scope and should therefore never be used as a variable or parameter name.
21+
The variable `$Error` is an automatic variables that exists in the global scope and should therefore
22+
never be used as a variable or parameter name.
2023

21-
``` PowerShell
24+
```powershell
2225
function foo($Error){ }
2326
```
2427

25-
``` PowerShell
28+
```powershell
2629
function Get-CustomErrorMessage($ErrorMessage){ $Error = "Error occurred: $ErrorMessage" }
2730
```
2831

2932
### Correct
3033

31-
``` PowerShell
34+
```powershell
3235
function Get-CustomErrorMessage($ErrorMessage){ $FinalErrorMessage = "Error occurred: $ErrorMessage" }
3336
```

RuleDocumentation/AvoidDefaultValueForMandatoryParameter.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
## Description
66

7-
Mandatory parameters should not have a default values because there is no scenario where the default can be used because `PowerShell` will prompt anyway if the parameter value is not specified when calling the function.
7+
Mandatory parameters should not have a default values because there is no scenario where the default
8+
can be used. PowerShell prompts for a value if the parameter value is not specified when calling the
9+
function.
810

911
## Example
1012

1113
### Wrong
1214

13-
``` PowerShell
15+
```powershell
1416
function Test
1517
{
1618
@@ -25,7 +27,7 @@ function Test
2527

2628
### Correct
2729

28-
``` PowerShell
30+
```powershell
2931
function Test
3032
{
3133
[CmdletBinding()]

RuleDocumentation/AvoidDefaultValueSwitchParameter.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Change the default value of the switch parameter to be false.
1414

1515
### Wrong
1616

17-
``` PowerShell
17+
```powershell
1818
function Test-Script
1919
{
2020
[CmdletBinding()]
@@ -32,7 +32,7 @@ function Test-Script
3232

3333
### Correct
3434

35-
``` PowerShell
35+
```powershell
3636
function Test-Script
3737
{
3838
[CmdletBinding()]

RuleDocumentation/AvoidGlobalAliases.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
## Description
66

7-
Globally scoped aliases override existing aliases within the sessions with matching names. This name collision can cause difficult to debug issues for consumers of modules and scripts.
7+
Globally scoped aliases override existing aliases within the sessions with matching names. This name
8+
collision can cause difficult to debug issues for consumers of modules and scripts.
89

9-
To understand more about scoping, see ```Get-Help about_Scopes```.
10+
To understand more about scoping, see `Get-Help about_Scopes`.
1011

11-
**NOTE** This rule is not available in PowerShell version 3 and 4 due to the `StaticParameterBinder.BindCommand` API that the rule uses internally.
12+
**NOTE** This rule is not available in PowerShell version 3 or 4 because it uses the
13+
`StaticParameterBinder.BindCommand` API.
1214

1315
## How
1416

@@ -18,12 +20,12 @@ Use other scope modifiers for new aliases.
1820

1921
### Wrong
2022

21-
``` PowerShell
23+
```powershell
2224
New-Alias -Name Name -Value Value -Scope "Global"
2325
```
2426

2527
### Correct
2628

27-
``` PowerShell
29+
```powershell
2830
New-Alias -Name Name1 -Value Value
2931
```

RuleDocumentation/AvoidGlobalFunctions.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
## Description
66

7-
Globally scoped functions override existing functions within the sessions with matching names. This name collision can cause difficult to debug issues for consumers of modules.
7+
Globally scoped functions override existing functions within the sessions with matching names. This
8+
name collision can cause difficult to debug issues for consumers of modules.
89

910

10-
To understand more about scoping, see ```Get-Help about_Scopes```.
11+
To understand more about scoping, see `Get-Help about_Scopes`.
1112

1213
## How
1314

@@ -17,12 +18,12 @@ Use other scope modifiers for functions.
1718

1819
### Wrong
1920

20-
``` PowerShell
21+
```powershell
2122
function global:functionName {}
2223
```
2324

2425
### Correct
2526

26-
``` PowerShell
27-
function functionName {}
27+
```powershell
28+
function functionName {}
2829
```

RuleDocumentation/AvoidGlobalVars.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
## Description
66

7-
A variable is a unit of memory in which values are stored. Windows PowerShell controls access to variables, functions, aliases, and drives through a mechanism known as scoping.
8-
Variables and functions that are present when Windows PowerShell starts have been created in the global scope.
7+
A variable is a unit of memory in which values are stored. PowerShell controls access to variables,
8+
functions, aliases, and drives through a mechanism known as scoping. Variables and functions that
9+
are present when PowerShell starts have been created in the global scope.
910

1011
Globally scoped variables include:
11-
* Automatic variables
12-
* Preference variables
13-
* Variables, aliases, and functions that are in your Windows PowerShell profiles
1412

15-
To understand more about scoping, see ```Get-Help about_Scopes```.
13+
- Automatic variables
14+
- Preference variables
15+
- Variables, aliases, and functions that are in your PowerShell profiles
16+
17+
To understand more about scoping, see `Get-Help about_Scopes`.
1618

1719
## How
1820

@@ -22,20 +24,20 @@ Use other scope modifiers for variables.
2224

2325
### Wrong
2426

25-
``` PowerShell
27+
```powershell
2628
$Global:var1 = $null
2729
function Test-NotGlobal ($var)
2830
{
29-
$a = $var + $var1
31+
$a = $var + $var1
3032
}
3133
```
3234

3335
### Correct
3436

35-
``` PowerShell
37+
```powershell
3638
$var1 = $null
3739
function Test-NotGlobal ($var1, $var2)
3840
{
39-
$a = $var1 + $var2
41+
$a = $var1 + $var2
4042
}
4143
```

RuleDocumentation/AvoidInvokingEmptyMembers.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
## Description
66

7-
Invoking non-constant members can cause potential bugs. Please double check the syntax to make sure that invoked members are constants.
7+
Invoking non-constant members can cause potential bugs. Please double check the syntax to make sure
8+
that invoked members are constants.
89

910
## How
1011

@@ -14,14 +15,14 @@ Provide the requested members for a given type or class.
1415

1516
### Wrong
1617

17-
``` PowerShell
18+
```powershell
1819
$MyString = "abc"
1920
$MyString.('len'+'gth')
2021
```
2122

2223
### Correct
2324

24-
``` PowerShell
25+
```powershell
2526
$MyString = "abc"
2627
$MyString.('length')
2728
```

0 commit comments

Comments
 (0)