@@ -31,14 +31,19 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
3131 /// <summary>
3232 /// InvokeScriptAnalyzerCommand: Cmdlet to statically check PowerShell scripts.
3333 /// </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" ) ]
3538 public class InvokeScriptAnalyzerCommand : PSCmdlet , IOutputWriter
3639 {
3740 #region Parameters
3841 /// <summary>
3942 /// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
4043 /// </summary>
41- [ Parameter ( Position = 0 , Mandatory = true ) ]
44+ [ Parameter ( Position = 0 ,
45+ ParameterSetName = "File" ,
46+ Mandatory = true ) ]
4247 [ ValidateNotNull ]
4348 [ Alias ( "PSPath" ) ]
4449 public string Path
@@ -48,18 +53,57 @@ public string Path
4853 }
4954 private string path ;
5055
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+
5170 /// <summary>
5271 /// CustomRulePath: The path to the file containing custom rules to run.
5372 /// </summary>
5473 [ Parameter ( Mandatory = false ) ]
5574 [ ValidateNotNull ]
5675 [ 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
5890 {
59- get { return customizedRulePath ; }
60- set { customizedRulePath = value ; }
91+ get { return recurseCustomRulePath ; }
92+ set { recurseCustomRulePath = value ; }
6193 }
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 ;
63107
64108 /// <summary>
65109 /// ExcludeRule: Array of names of rules to be disabled.
@@ -125,16 +169,20 @@ public SwitchParameter SuppressedOnly
125169 private bool suppressedOnly ;
126170
127171 /// <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
129173 /// </summary>
174+ [ Alias ( "Profile" ) ]
130175 [ Parameter ( Mandatory = false ) ]
131176 [ ValidateNotNull ]
132- public string Profile
177+ public object Settings
133178 {
134- get { return profile ; }
135- set { profile = value ; }
179+ get { return settings ; }
180+ set { settings = value ; }
136181 }
137- private string profile ;
182+
183+ private object settings ;
184+
185+ private bool stopProcessing ;
138186
139187 #endregion Parameters
140188
@@ -145,37 +193,79 @@ public string Profile
145193 /// </summary>
146194 protected override void BeginProcessing ( )
147195 {
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+
148205 ScriptAnalyzer . Instance . Initialize (
149206 this ,
150- customizedRulePath ,
207+ rulePaths ,
151208 this . includeRule ,
152209 this . excludeRule ,
153210 this . severity ,
154- this . suppressedOnly ,
155- this . profile ) ;
211+ null == rulePaths ? true : this . includeDefaultRules ,
212+ this . suppressedOnly ) ;
156213 }
157214
158215 /// <summary>
159216 /// Analyzes the given script/directory.
160217 /// </summary>
161218 protected override void ProcessRecord ( )
162219 {
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 ) )
166236 {
167- ProcessPath ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
237+ ProcessPathOrScriptDefinition ( scriptDefinition ) ;
168238 }
169239 }
170240
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+
171253 #endregion
172254
173255 #region Methods
174256
175- private void ProcessPath ( string path )
257+ private void ProcessPathOrScriptDefinition ( string pathOrScriptDefinition )
176258 {
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+ }
179269
180270 //Output through loggers
181271 foreach ( ILogger logger in ScriptAnalyzer . Instance . Loggers )
0 commit comments