Skip to content

Commit 4efa348

Browse files
Adding documentation about suppressing rule violations.
1 parent b4e890c commit 4efa348

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,86 @@ If you have previous version of PSScriptAnalyzer installed on your machine, you
3939

4040
To confirm installation: run ```Get-ScriptAnalyzerRule``` in the PowerShell console to obtain the built-in rules
4141

42+
Suppressing Rules
43+
=================
44+
45+
You can suppress a rule by decorating a script/function or script/function parameter with .NET's [SuppressMessageAttribute](https://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.aspx). `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 (you may omit the `checkID` parameter):
46+
47+
function SuppressMe()
48+
{
49+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp")]
50+
param()
51+
52+
Write-Verbose -Message "I'm making a difference!"
53+
54+
}
55+
56+
To suppress a message on a specific parameter, set the `SuppressMessageAttribute`'s `CheckId` parameter to the name of the parameter:
57+
58+
function SuppressTwoVariables()
59+
{
60+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "b")]
61+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "a")]
62+
param([string]$a, [int]$b)
63+
{
64+
}
65+
}
66+
67+
To suppress a rule for an entire function/script, decorate the `param` block of the script/function and set the `SuppressMessageAttribute's` `Scope` property to `Function`:
68+
69+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
70+
param(
71+
)
72+
73+
74+
You can also suppress a rule for an entire class using `Class` as the value of the `Scope` property:
75+
76+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
77+
class TestClass
78+
{
79+
<#
80+
... snip ...
81+
#>
82+
}
83+
84+
Finally, you can restrict suppression inside a scope by setting the `SuppressMessageAttribute's` `Target` property to a regular expression that causes the script analyzer to skip functions/variables/parameters/objects whose names match the regular expression.
85+
86+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")]
87+
Param(
88+
)
89+
90+
function PositionalParametersAllowed()
91+
{
92+
Param([string]$Parameter1)
93+
{
94+
Write-Verbose $Parameter1
95+
}
96+
97+
}
98+
99+
function PositionalParametersNotAllowed()
100+
{
101+
param([string]$Parameter1)
102+
{
103+
Write-Verbose $Parameter1
104+
}
105+
}
106+
107+
# The script analyzer will skip this violation
108+
PositionalParametersAllowed 'value1'
109+
110+
# The script analyzer will report this violation
111+
PositionalParametersNotAllowed 'value1
112+
113+
To match all functions/variables/parameters/objects, use `*` as the value of the Target parameter:
114+
115+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="*")]
116+
Param(
117+
)
118+
119+
120+
121+
42122
Building the Code
43123
=================
44124

0 commit comments

Comments
 (0)