Skip to content

Commit 649fb23

Browse files
committed
Add documentation to all rules supporting a custom suppression target
A subset of PSScriptAnalyzer rules support customising a suppression so that it only applies to a specific target. This is performed through the second argument to the `SuppressMessageAttribute` constructor, which is otherwise an empty string to apply the rule globally within its scope. This commit adds documentation to all rules which support customising the target of the suppression, which until now was largely undocumented.
1 parent 411c3d0 commit 649fb23

22 files changed

Lines changed: 528 additions & 64 deletions

‎docs/Rules/AvoidAssignmentToAutomaticVariable.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ this rule:
5454
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
5555
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
5656

57+
## Suppression
58+
59+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
60+
block of a function or script. An empty string as the second argument suppresses every violation
61+
of the rule in that scope.
62+
63+
```powershell
64+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '')]
65+
```
66+
67+
To suppress the rule for a single automatic variable, pass the variable name without the leading
68+
`$` as the second argument. The name isn't case sensitive.
69+
70+
```powershell
71+
function Test-Script {
72+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', 'Error')]
73+
param(
74+
[String]$ErrorMessage
75+
)
76+
77+
$Error = "Error occurred: $ErrorMessage"
78+
}
79+
```
80+
5781
<!-- Link references -->
5882
[01]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables
59-
[02]: ../using-scriptanalyzer.md
83+
[02]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidDefaultValueForMandatoryParameter.md‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,30 @@ this rule:
5656
- Create a custom rule configuration file to include only the rules you want or exclude the rules
5757
you don't want.
5858
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
59-
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
59+
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][01].
60+
61+
## Suppression
62+
63+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
64+
block of a function or script. An empty string as the second argument suppresses every violation
65+
of the rule in that scope.
66+
67+
```powershell
68+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidDefaultValueForMandatoryParameter', '')]
69+
```
70+
71+
To suppress the rule for a single parameter, pass the parameter name without the leading `$` as
72+
the second argument. The name isn't case sensitive.
73+
74+
```powershell
75+
function Test-Script {
76+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidDefaultValueForMandatoryParameter', 'Parameter')]
77+
param(
78+
[Parameter(Mandatory)]
79+
$Parameter = 'Default Value'
80+
)
81+
}
82+
```
6083

6184
<!-- Link references -->
62-
[02]: ../using-scriptanalyzer.md
85+
[01]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidDynamicallyCreatingVariableNames.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ Rules = @{
6969

7070
Enable or disable the rule during ScriptAnalyzer invocation.
7171

72+
## Suppression
73+
74+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
75+
block of a function or script. An empty string as the second argument suppresses every violation
76+
of the rule in that scope.
77+
78+
```powershell
79+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidDynamicallyCreatingVariableNames', '')]
80+
```
81+
82+
To suppress the rule for a single `New-Variable` call, pass the value of its `Name` argument
83+
exactly as it's written in the call, without any enclosing double quotes, as the second argument.
84+
For example, use `'My$_'` for `New-Variable -Name "My$_"` and `'$name'` for
85+
`New-Variable -Name $name`. Enclose the second argument in single quotes so that PowerShell
86+
doesn't try to expand it.
87+
88+
```powershell
89+
function Test-Script {
90+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidDynamicallyCreatingVariableNames', 'My$_')]
91+
param()
92+
93+
'One', 'Two', 'Three' | ForEach-Object -Begin { $i = 1 } -Process {
94+
New-Variable -Name "My$_" -Value ($i++)
95+
}
96+
}
97+
```
98+
7299
## References
73100

74101
- [New-Variable][02]

‎docs/Rules/AvoidGlobalFunctions.md‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ title: AvoidGlobalFunctions
1212

1313
## Description
1414

15-
This rule detects function definitions that use the `global:` scope modifier on the function name to
15+
This rule detects function definitions that use the `Global:` scope modifier on the function name to
1616
define a function in the global scope. Global functions can unintentionally override existing
1717
functions in the session, leading to unexpected behavior and name collisions. Name collisions make
1818
it difficult for module consumers to diagnose issues and maintain code reliability.
@@ -25,7 +25,7 @@ scope modifiers. To learn more, see [about_Scopes][01].
2525
### Noncompliant
2626

2727
```powershell
28-
function global:functionName {}
28+
function Global:functionName {}
2929
```
3030

3131
### Compliant
@@ -44,6 +44,27 @@ this rule:
4444
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
4545
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
4646

47+
## Suppression
48+
49+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
50+
block of a function or script. An empty string as the second argument suppresses every violation
51+
of the rule in that scope.
52+
53+
```powershell
54+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalFunctions', '')]
55+
```
56+
57+
To suppress the rule for a single function, pass the function name, including the `Global:` scope
58+
modifier, as the second argument. The name isn't case sensitive. This is useful when the attribute
59+
is applied at module scope and covers more than one function.
60+
61+
```powershell
62+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalFunctions', 'Global:Test-Script')]
63+
param()
64+
65+
function Global:Test-Script {}
66+
```
67+
4768
<!-- link references -->
4869
[01]: /powershell/module/microsoft.powershell.core/about/about_scopes
49-
[02]: ../using-scriptanalyzer.md
70+
[02]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidGlobalVars.md‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ variables include:
2626

2727
This rule doesn't detect use of the `New-Variable` cmdlet to create variables in the global scope or
2828
the other `*-Variable` cmdlets to work with variables in the global scope. It only detects variable
29-
expressions with the global scope modifier, like `$global:example`.
29+
expressions with the global scope modifier, like `$Global:example`.
3030

3131
This rule doesn't apply to variable expressions that use the global scope modifier where the
3232
variable name is for an [automatic variable][01] or a [preference variable][02].
@@ -68,8 +68,32 @@ this rule:
6868
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
6969
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][04].
7070

71+
## Suppression
72+
73+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
74+
block of a function or script. An empty string as the second argument suppresses every violation
75+
of the rule in that scope.
76+
77+
```powershell
78+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
79+
```
80+
81+
To suppress the rule for a single variable, pass the variable name, including the `Global:` scope
82+
modifier, as the second argument. The name isn't case sensitive.
83+
84+
```powershell
85+
function Test-Script {
86+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', 'Global:Variable')]
87+
param(
88+
[String]$Parameter
89+
)
90+
91+
$Global:Variable = $Parameter
92+
}
93+
```
94+
7195
<!-- link references -->
7296
[01]: /powershell/module/microsoft.powershell.core/about/about_automatic_variables
7397
[02]: /powershell/module/microsoft.powershell.core/about/about_preference_variables
7498
[03]: /powershell/module/microsoft.powershell.core/about/about_scopes
75-
[04]: ../using-scriptanalyzer.md
99+
[04]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidNullOrEmptyHelpMessageAttribute.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ this rule:
8383
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
8484
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
8585

86+
## Suppression
87+
88+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
89+
block of a function or script. An empty string as the second argument suppresses every violation
90+
of the rule in that scope.
91+
92+
```powershell
93+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidNullOrEmptyHelpMessageAttribute', '')]
94+
```
95+
96+
To suppress the rule for a single parameter, pass the parameter name without the leading `$` as
97+
the second argument. The name isn't case sensitive.
98+
99+
```powershell
100+
function Test-Script {
101+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidNullOrEmptyHelpMessageAttribute', 'Parameter')]
102+
param(
103+
[Parameter(HelpMessage = '')]
104+
[String]$Parameter
105+
)
106+
}
107+
```
108+
86109
<!-- link references -->
87110
[01]: /powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters#helpmessage-argument
88-
[02]: ../using-scriptanalyzer.md
111+
[02]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidUsingAllowUnencryptedAuthentication.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,30 @@ this rule:
4545
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
4646
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][03].
4747

48+
## Suppression
49+
50+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
51+
block of a function or script. An empty string as the second argument suppresses every violation
52+
of the rule in that scope.
53+
54+
```powershell
55+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingAllowUnencryptedAuthentication', '')]
56+
```
57+
58+
To suppress the rule for a single command, pass the command name as it appears in the call as the
59+
second argument. The name isn't case sensitive, but it must match what's written in the script, so
60+
a call made through an alias needs the alias name.
61+
62+
```powershell
63+
function Test-Script {
64+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingAllowUnencryptedAuthentication', 'Invoke-WebRequest')]
65+
param()
66+
67+
Invoke-WebRequest foo -AllowUnencryptedAuthentication
68+
}
69+
```
70+
4871
<!-- Link references -->
4972
[01]: /powershell/module/microsoft.powershell.utility/invoke-webrequest
5073
[02]: /powershell/module/microsoft.powershell.utility/invoke-restmethod
51-
[03]: ../using-scriptanalyzer.md
74+
[03]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidUsingBrokenHashAlgorithms.md‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,30 @@ this rule:
4141
- Create a custom rule configuration file to include only the rules you want or exclude the rules
4242
you don't want.
4343
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
44-
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
44+
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][01].
45+
46+
## Suppression
47+
48+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
49+
block of a function or script. An empty string as the second argument suppresses every violation
50+
of the rule in that scope.
51+
52+
```powershell
53+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingBrokenHashAlgorithms', '')]
54+
```
55+
56+
To suppress the rule for a single command, pass the command name as it appears in the call as the
57+
second argument. The name isn't case sensitive, but it must match what's written in the script, so
58+
a call made through an alias needs the alias name.
59+
60+
```powershell
61+
function Test-Script {
62+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingBrokenHashAlgorithms', 'Get-FileHash')]
63+
param()
64+
65+
Get-FileHash foo.txt -Algorithm MD5
66+
}
67+
```
4568

4669
<!-- Link references -->
47-
[02]: ../using-scriptanalyzer.md
70+
[01]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidUsingCmdletAliases.md‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,31 @@ using this rule:
6565
- Create a custom rule configuration file to include only the rules you want or exclude the rules
6666
you don't want.
6767
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
68-
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
68+
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][01].
69+
70+
## Suppression
71+
72+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
73+
block of a function or script. An empty string as the second argument suppresses every violation
74+
of the rule in that scope.
75+
76+
```powershell
77+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases', '')]
78+
```
79+
80+
To suppress the rule for a single alias, pass the alias as the second argument. For an implicit
81+
alias, pass the command name as it's written without the `Get-` prefix, for example `'verb'` for
82+
`verb`. The name isn't case sensitive. To allow an alias everywhere, use the `allowlist` setting
83+
instead.
84+
85+
```powershell
86+
function Test-Script {
87+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases', 'gps')]
88+
param()
89+
90+
gps | Where-Object { $_.WorkingSet -gt 20000000 }
91+
}
92+
```
6993

7094
<!-- Link references -->
71-
[02]: ../using-scriptanalyzer.md
95+
[01]: ../using-scriptanalyzer.md#suppressing-rules

‎docs/Rules/AvoidUsingComputerNameHardcoded.md‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,30 @@ this rule:
4848
- Create a custom rule configuration file to include only the rules you want or exclude the rules
4949
you don't want.
5050
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific
51-
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][02].
51+
code blocks. For more information, see the _Suppressing rules_ section of [Using PSScriptAnalyzer][01].
52+
53+
## Suppression
54+
55+
As with other rules, you can suppress this rule by adding a suppression attribute to the `param`
56+
block of a function or script. An empty string as the second argument suppresses every violation
57+
of the rule in that scope.
58+
59+
```powershell
60+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingComputerNameHardcoded', '')]
61+
```
62+
63+
To suppress the rule for a single command, pass the command name as it appears in the call as the
64+
second argument. The name isn't case sensitive, but it must match what's written in the script, so
65+
a call made through an alias needs the alias name.
66+
67+
```powershell
68+
function Test-Script {
69+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingComputerNameHardcoded', 'Invoke-Command')]
70+
param()
71+
72+
Invoke-Command -ComputerName HardcodedHostname -Port 343
73+
}
74+
```
5275

5376
<!-- Link references -->
54-
[02]: ../using-scriptanalyzer.md
77+
[01]: ../using-scriptanalyzer.md#suppressing-rules

0 commit comments

Comments
 (0)