Skip to content

Commit 1510362

Browse files
committed
Fix for PowerShell#475 - Updated profile example to work.
1 parent 2df55e0 commit 1510362

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

README.md

+43-31
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Installation
6262
```powershell
6363
Import-Module PSScriptAnalyzer
6464
```
65-
If you have previous version of PSScriptAnalyzer installed on your machine, you may need to override old binaries by copying content of [``~/ProjectRoot/PSScriptAnalyzer``] to PSModulePath.
65+
If you have previous version of PSScriptAnalyzer installed on your machine, you may need to override old binaries by copying content of [``~/ProjectRoot/PSScriptAnalyzer``] to PSModulePath.
6666

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

@@ -78,11 +78,11 @@ You can suppress a rule by decorating a script/function or script/function param
7878
param()
7979

8080
Write-Verbose -Message "I'm making a difference!"
81-
81+
8282
}
83-
83+
8484
All rule violations within the scope of the script/function/parameter you decorate will be suppressed.
85-
85+
8686
To suppress a message on a specific parameter, set the `SuppressMessageAttribute`'s `CheckId` parameter to the name of the parameter:
8787

8888
function SuppressTwoVariables()
@@ -100,45 +100,45 @@ Use the `SuppressMessageAttribute`'s `Scope` property to limit rule suppression
100100
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
101101
param(
102102
)
103-
103+
104104
function InternalFunction
105105
{
106106
param()
107-
107+
108108
Write-Verbose -Message "I am invincible!"
109109
}
110-
110+
111111
The above example demonstrates how to suppress rule violations for internal functions using the `SuppressMessageAttribute`'s `Scope` property.
112112

113113
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. Any function/parameter/class/variable/object whose name matches the regular expression is skipped.
114114

115115
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")]
116116
Param(
117117
)
118-
118+
119119
function PositionalParametersAllowed()
120120
{
121121
Param([string]$Parameter1)
122122
{
123123
Write-Verbose $Parameter1
124124
}
125-
125+
126126
}
127-
127+
128128
function PositionalParametersNotAllowed()
129129
{
130130
param([string]$Parameter1)
131131
{
132132
Write-Verbose $Parameter1
133133
}
134134
}
135-
135+
136136
# The script analyzer will skip this violation
137137
PositionalParametersAllowed 'value1'
138-
138+
139139
# The script analyzer will report this violation
140140
PositionalParametersNotAllowed 'value1
141-
141+
142142
To match all functions/variables/parameters/objects, use `*` as the value of the Target parameter:
143143

144144
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="*")]
@@ -154,18 +154,30 @@ Profiles that describe ScriptAnalyzer rules to include/exclude based on `Severit
154154

155155
Using Profile support:
156156

157+
157158
```powershell
158-
$myProfile = @{
159-
Severity='Warning'
160-
IncludeRules=@('PSAvoidUsingCmdletAliases',
161-
'PSAvoidUsingPositionalParameters',
162-
'PSAvoidUsingInternalURLs'
163-
'PSAvoidUninitializedVariable')
164-
ExcludeRules=@('PSAvoidUsingCmdletAliases'
165-
'PSAvoidUninitializedVariable')
159+
# This example excludes two rules from the default set of rules and any rule
160+
# that does not output an Error or Warning diagnostic record.
161+
@'
162+
@{
163+
Severity=@('Error','Warning')
164+
ExcludeRules=@('PSAvoidUsingCmdletAliases',
165+
'PSAvoidUsingWriteHost')
166166
}
167+
'@ > ScriptAnalyzerProfile.psd1
167168
168-
Invoke-ScriptAnalyzer -path MyScript.ps1 -Profile $myProfile
169+
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Profile ScriptAnalyzerProfile.psd1
170+
171+
# This example selects just a few rules to execute instead of all the default
172+
# rules.
173+
@'
174+
@{
175+
IncludeRules=@('PSAvoidUsingPlainTextForPassword',
176+
'PSAvoidUsingConvertToSecureStringWithPlainText')
177+
}
178+
'@ > ScriptAnalyzerProfile.psd1
179+
180+
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Profile ScriptAnalyzerProfile.psd1
169181
```
170182

171183
ScriptAnalyzer as a .net library
@@ -179,18 +191,18 @@ Here are the public interfaces:
179191
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
180192

181193
public void Initialize(System.Management.Automation.Runspaces.Runspace runspace,
182-
Microsoft.Windows.PowerShell.ScriptAnalyzer.IOutputWriter outputWriter,
183-
[string[] customizedRulePath = null],
184-
[string[] includeRuleNames = null],
185-
[string[] excludeRuleNames = null],
186-
[string[] severity = null],
187-
[bool suppressedOnly = false],
194+
Microsoft.Windows.PowerShell.ScriptAnalyzer.IOutputWriter outputWriter,
195+
[string[] customizedRulePath = null],
196+
[string[] includeRuleNames = null],
197+
[string[] excludeRuleNames = null],
198+
[string[] severity = null],
199+
[bool suppressedOnly = false],
188200
[string profile = null])
189201

190-
public System.Collections.Generic.IEnumerable<DiagnosticRecord> AnalyzePath(string path,
202+
public System.Collections.Generic.IEnumerable<DiagnosticRecord> AnalyzePath(string path,
191203
[bool searchRecursively = false])
192204

193-
public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleNames,
205+
public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleNames,
194206
string[] ruleNames)
195207
```
196208

@@ -215,7 +227,7 @@ Pester-based ScriptAnalyzer Tests are located in ```<branch>/PSScriptAnalyzer/Te
215227
* Run Tests for Built-in rules:
216228
.\*.ps1 (Example - .\ AvoidConvertToSecureStringWithPlainText.ps1)
217229
*You can also run all tests under \Engine or \Rules by calling Invoke-Pester in the Engine/Rules directory.
218-
230+
219231
Project Management Dashboard
220232
==============================
221233

0 commit comments

Comments
 (0)