@@ -31,14 +31,19 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
31
31
/// <summary>
32
32
/// InvokeScriptAnalyzerCommand: Cmdlet to statically check PowerShell scripts.
33
33
/// </summary>
34
- [ Cmdlet ( VerbsLifecycle . Invoke , "ScriptAnalyzer" , HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525914" ) ]
34
+ [ Cmdlet ( VerbsLifecycle . Invoke ,
35
+ "ScriptAnalyzer" ,
36
+ DefaultParameterSetName = "File" ,
37
+ HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525914" ) ]
35
38
public class InvokeScriptAnalyzerCommand : PSCmdlet , IOutputWriter
36
39
{
37
40
#region Parameters
38
41
/// <summary>
39
42
/// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
40
43
/// </summary>
41
- [ Parameter ( Position = 0 , Mandatory = true ) ]
44
+ [ Parameter ( Position = 0 ,
45
+ ParameterSetName = "File" ,
46
+ Mandatory = true ) ]
42
47
[ ValidateNotNull ]
43
48
[ Alias ( "PSPath" ) ]
44
49
public string Path
@@ -48,18 +53,57 @@ public string Path
48
53
}
49
54
private string path ;
50
55
56
+ /// <summary>
57
+ /// ScriptDefinition: a script definition in the form of a string to run rules on.
58
+ /// </summary>
59
+ [ Parameter ( Position = 0 ,
60
+ ParameterSetName = "ScriptDefinition" ,
61
+ Mandatory = true ) ]
62
+ [ ValidateNotNull ]
63
+ public string ScriptDefinition
64
+ {
65
+ get { return scriptDefinition ; }
66
+ set { scriptDefinition = value ; }
67
+ }
68
+ private string scriptDefinition ;
69
+
51
70
/// <summary>
52
71
/// CustomRulePath: The path to the file containing custom rules to run.
53
72
/// </summary>
54
73
[ Parameter ( Mandatory = false ) ]
55
74
[ ValidateNotNull ]
56
75
[ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
57
- public string [ ] CustomizedRulePath
76
+ [ Alias ( "CustomizedRulePath" ) ]
77
+ public string [ ] CustomRulePath
78
+ {
79
+ get { return customRulePath ; }
80
+ set { customRulePath = value ; }
81
+ }
82
+ private string [ ] customRulePath ;
83
+
84
+ /// <summary>
85
+ /// RecurseCustomRulePath: Find rules within subfolders under the path
86
+ /// </summary>
87
+ [ Parameter ( Mandatory = false ) ]
88
+ [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
89
+ public SwitchParameter RecurseCustomRulePath
58
90
{
59
- get { return customizedRulePath ; }
60
- set { customizedRulePath = value ; }
91
+ get { return recurseCustomRulePath ; }
92
+ set { recurseCustomRulePath = value ; }
61
93
}
62
- private string [ ] customizedRulePath ;
94
+ private bool recurseCustomRulePath ;
95
+
96
+ /// <summary>
97
+ /// IncludeDefaultRules: Invoke default rules along with Custom rules
98
+ /// </summary>
99
+ [ Parameter ( Mandatory = false ) ]
100
+ [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
101
+ public SwitchParameter IncludeDefaultRules
102
+ {
103
+ get { return includeDefaultRules ; }
104
+ set { includeDefaultRules = value ; }
105
+ }
106
+ private bool includeDefaultRules ;
63
107
64
108
/// <summary>
65
109
/// ExcludeRule: Array of names of rules to be disabled.
@@ -125,16 +169,20 @@ public SwitchParameter SuppressedOnly
125
169
private bool suppressedOnly ;
126
170
127
171
/// <summary>
128
- /// Returns path to the file that contains user profile for ScriptAnalyzer
172
+ /// Returns path to the file that contains user profile or hash table for ScriptAnalyzer
129
173
/// </summary>
174
+ [ Alias ( "Profile" ) ]
130
175
[ Parameter ( Mandatory = false ) ]
131
176
[ ValidateNotNull ]
132
- public string Profile
177
+ public object Settings
133
178
{
134
- get { return profile ; }
135
- set { profile = value ; }
179
+ get { return settings ; }
180
+ set { settings = value ; }
136
181
}
137
- private string profile ;
182
+
183
+ private object settings ;
184
+
185
+ private bool stopProcessing ;
138
186
139
187
#endregion Parameters
140
188
@@ -145,37 +193,79 @@ public string Profile
145
193
/// </summary>
146
194
protected override void BeginProcessing ( )
147
195
{
196
+ string [ ] rulePaths = Helper . ProcessCustomRulePaths ( customRulePath ,
197
+ this . SessionState , recurseCustomRulePath ) ;
198
+
199
+ if ( ! ScriptAnalyzer . Instance . ParseProfile ( this . settings , this . SessionState . Path , this ) )
200
+ {
201
+ stopProcessing = true ;
202
+ return ;
203
+ }
204
+
148
205
ScriptAnalyzer . Instance . Initialize (
149
206
this ,
150
- customizedRulePath ,
207
+ rulePaths ,
151
208
this . includeRule ,
152
209
this . excludeRule ,
153
210
this . severity ,
154
- this . suppressedOnly ,
155
- this . profile ) ;
211
+ null == rulePaths ? true : this . includeDefaultRules ,
212
+ this . suppressedOnly ) ;
156
213
}
157
214
158
215
/// <summary>
159
216
/// Analyzes the given script/directory.
160
217
/// </summary>
161
218
protected override void ProcessRecord ( )
162
219
{
163
- // throws Item Not Found Exception
164
- Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
165
- foreach ( PathInfo p in paths )
220
+ if ( stopProcessing )
221
+ {
222
+ stopProcessing = false ;
223
+ return ;
224
+ }
225
+
226
+ if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
227
+ {
228
+ // throws Item Not Found Exception
229
+ Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
230
+ foreach ( PathInfo p in paths )
231
+ {
232
+ ProcessPathOrScriptDefinition ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
233
+ }
234
+ }
235
+ else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
166
236
{
167
- ProcessPath ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
237
+ ProcessPathOrScriptDefinition ( scriptDefinition ) ;
168
238
}
169
239
}
170
240
241
+ protected override void EndProcessing ( )
242
+ {
243
+ ScriptAnalyzer . Instance . CleanUp ( ) ;
244
+ base . EndProcessing ( ) ;
245
+ }
246
+
247
+ protected override void StopProcessing ( )
248
+ {
249
+ ScriptAnalyzer . Instance . CleanUp ( ) ;
250
+ base . StopProcessing ( ) ;
251
+ }
252
+
171
253
#endregion
172
254
173
255
#region Methods
174
256
175
- private void ProcessPath ( string path )
257
+ private void ProcessPathOrScriptDefinition ( string pathOrScriptDefinition )
176
258
{
177
- IEnumerable < DiagnosticRecord > diagnosticsList =
178
- ScriptAnalyzer . Instance . AnalyzePath ( path , this . recurse ) ;
259
+ IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
260
+
261
+ if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
262
+ {
263
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath ( pathOrScriptDefinition , this . recurse ) ;
264
+ }
265
+ else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
266
+ {
267
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( pathOrScriptDefinition ) ;
268
+ }
179
269
180
270
//Output through loggers
181
271
foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
0 commit comments