@@ -38,6 +38,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
38
38
HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525914" ) ]
39
39
public class InvokeScriptAnalyzerCommand : PSCmdlet , IOutputWriter
40
40
{
41
+ #region Private variables
42
+ List < string > processedPaths ;
43
+ #endregion // Private variables
44
+
41
45
#region Parameters
42
46
/// <summary>
43
47
/// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
@@ -218,12 +222,24 @@ protected override void BeginProcessing()
218
222
219
223
string [ ] rulePaths = Helper . ProcessCustomRulePaths ( customRulePath ,
220
224
this . SessionState , recurseCustomRulePath ) ;
225
+ if ( IsFileParameterSet ( ) )
226
+ {
227
+ ProcessPath ( ) ;
228
+ }
221
229
222
230
var settingFileHasErrors = false ;
223
- if ( settings == null )
231
+ if ( settings == null
232
+ && processedPaths != null
233
+ && processedPaths . Count == 1 )
224
234
{
225
235
// add a directory separator character because if there is no trailing separator character, it will return the parent
226
- var directory = System . IO . Path . GetDirectoryName ( path + System . IO . Path . DirectorySeparatorChar ) ;
236
+ var directory = processedPaths [ 0 ] . TrimEnd ( System . IO . Path . DirectorySeparatorChar ) ;
237
+ if ( File . Exists ( directory ) )
238
+ {
239
+ // if given path is a file, get its directory
240
+ directory = System . IO . Path . GetDirectoryName ( directory ) ;
241
+ }
242
+
227
243
this . WriteVerbose (
228
244
String . Format (
229
245
"Settings not provided. Will look for settings file in the given path {0}." ,
@@ -244,7 +260,6 @@ protected override void BeginProcessing()
244
260
settingsFilename ,
245
261
directory ) ) ;
246
262
settingFileHasErrors = ! ScriptAnalyzer . Instance . ParseProfile ( settingsFilepath , this . SessionState . Path , this ) ;
247
-
248
263
}
249
264
}
250
265
@@ -330,15 +345,11 @@ protected override void StopProcessing()
330
345
private void ProcessInput ( )
331
346
{
332
347
IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
333
- if ( String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) )
348
+ if ( IsFileParameterSet ( ) )
334
349
{
335
- // throws Item Not Found Exception
336
- Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
337
- foreach ( PathInfo p in paths )
350
+ foreach ( var p in processedPaths )
338
351
{
339
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath (
340
- this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ,
341
- this . recurse ) ;
352
+ diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath ( p , this . recurse ) ;
342
353
WriteToOutput ( diagnosticsList ) ;
343
354
}
344
355
}
@@ -359,6 +370,21 @@ private void WriteToOutput(IEnumerable<DiagnosticRecord> diagnosticRecords)
359
370
}
360
371
}
361
372
}
373
+
374
+ private void ProcessPath ( )
375
+ {
376
+ Collection < PathInfo > paths = this . SessionState . Path . GetResolvedPSPathFromPSPath ( path ) ;
377
+ processedPaths = new List < string > ( ) ;
378
+ foreach ( PathInfo p in paths )
379
+ {
380
+ processedPaths . Add ( this . SessionState . Path . GetUnresolvedProviderPathFromPSPath ( p . Path ) ) ;
381
+ }
382
+ }
383
+
384
+ private bool IsFileParameterSet ( )
385
+ {
386
+ return String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) ;
387
+ }
362
388
#endregion
363
389
}
364
390
}
0 commit comments