Skip to content

Commit 6cf6dcf

Browse files
committed
Changed section on Profile support to use Setting instead.
1 parent 5d9ff2b commit 6cf6dcf

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

README.md

Lines changed: 57 additions & 36 deletions
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,75 +100,96 @@ 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="*")]
145145
Param(
146146
)
147147

148148

149-
150-
Profile support in ScriptAnalyzer
149+
Settings Support in ScriptAnalyzer
151150
========================================
152151

153-
Profiles that describe ScriptAnalyzer rules to include/exclude based on `Severity` can be created and supplied to `Invoke-ScriptAnalyzer` using the `-profile` parameter. This enables a user to create custom configuration for a specific environment.
152+
Settings that describe ScriptAnalyzer rules to include/exclude based on `Severity` can be created and supplied to
153+
`Invoke-ScriptAnalyzer` using the `-Setting` parameter. This enables a user to create a custom configuration for a specific environment.
154+
155+
Using Settings support:
154156

155-
Using Profile support:
157+
The following example excludes two rules from the default set of rules and any rule
158+
that does not output an Error or Warning diagnostic record.
156159

157160
```powershell
158-
$myProfile = @{
159-
Severity='Warning'
160-
IncludeRules=@('PSAvoidUsingCmdletAliases',
161-
'PSAvoidUsingPositionalParameters',
162-
'PSAvoidUsingInternalURLs'
163-
'PSAvoidUninitializedVariable')
164-
ExcludeRules=@('PSAvoidUsingCmdletAliases'
165-
'PSAvoidUninitializedVariable')
161+
# ScriptAnalyzerSettings.psd1
162+
@{
163+
Severity=@('Error','Warning')
164+
ExcludeRules=@('PSAvoidUsingCmdletAliases',
165+
'PSAvoidUsingWriteHost')
166166
}
167+
```
168+
169+
Then invoke that settings file when using `Invoke-ScriptAnalyzer`:
170+
171+
```powershell
172+
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Setting ScriptAnalyzerSettings.psd1
173+
```
174+
175+
The next example selects a few rules to execute instead of all the default rules.
176+
177+
```powershell
178+
# ScriptAnalyzerSettings.psd1
179+
@{
180+
IncludeRules=@('PSAvoidUsingPlainTextForPassword',
181+
'PSAvoidUsingConvertToSecureStringWithPlainText')
182+
}
183+
```
184+
185+
Then invoke that settings file when using `Invoke-ScriptAnalyzer`:
167186

168-
Invoke-ScriptAnalyzer -path MyScript.ps1 -Profile $myProfile
187+
```powershell
188+
Invoke-ScriptAnalyzer -Path MyScript.ps1 -Setting ScriptAnalyzerSettings.psd1
169189
```
170190

171-
ScriptAnalyzer as a .net library
191+
192+
ScriptAnalyzer as a .NET library
172193
================================
173194

174195
ScriptAnalyzer engine and functionality can now be directly consumed as a library.
@@ -179,18 +200,18 @@ Here are the public interfaces:
179200
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
180201

181202
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],
203+
Microsoft.Windows.PowerShell.ScriptAnalyzer.IOutputWriter outputWriter,
204+
[string[] customizedRulePath = null],
205+
[string[] includeRuleNames = null],
206+
[string[] excludeRuleNames = null],
207+
[string[] severity = null],
208+
[bool suppressedOnly = false],
188209
[string profile = null])
189210

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

193-
public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleNames,
214+
public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleNames,
194215
string[] ruleNames)
195216
```
196217

@@ -215,7 +236,7 @@ Pester-based ScriptAnalyzer Tests are located in ```<branch>/PSScriptAnalyzer/Te
215236
* Run Tests for Built-in rules:
216237
.\*.ps1 (Example - .\ AvoidConvertToSecureStringWithPlainText.ps1)
217238
*You can also run all tests under \Engine or \Rules by calling Invoke-Pester in the Engine/Rules directory.
218-
239+
219240
Project Management Dashboard
220241
==============================
221242

0 commit comments

Comments
 (0)