@@ -346,39 +346,45 @@ private IEnumerable<PSObject> GetDiagnosticRecords<TSettings>(
346
346
ScriptFile file ,
347
347
string [ ] rules ,
348
348
TSettings settings ) where TSettings : class
349
+ {
350
+ var task = GetDiagnosticRecordsAsync ( file , rules , settings ) ;
351
+ task . Wait ( ) ;
352
+ return task . Result ;
353
+ }
354
+
355
+ private async Task < IEnumerable < PSObject > > GetDiagnosticRecordsAsync < TSettings > (
356
+ ScriptFile file ,
357
+ string [ ] rules ,
358
+ TSettings settings ) where TSettings : class
349
359
{
350
360
IEnumerable < PSObject > diagnosticRecords = Enumerable . Empty < PSObject > ( ) ;
351
361
352
362
if ( this . scriptAnalyzerModuleInfo != null
353
363
&& ( typeof ( TSettings ) == typeof ( string )
354
364
|| typeof ( TSettings ) == typeof ( Hashtable ) ) )
355
365
{
356
- lock ( runspaceLock )
366
+ //Use a settings file if one is provided, otherwise use the default rule list.
367
+ string settingParameter ;
368
+ object settingArgument ;
369
+ if ( settings != null )
357
370
{
358
- using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
359
- {
360
- powerShell . Runspace = this . analysisRunspace ;
361
- Logger . Write (
362
- LogLevel . Verbose ,
363
- String . Format ( "Running PSScriptAnalyzer against {0}" , file . FilePath ) ) ;
364
-
365
- powerShell
366
- . AddCommand ( "Invoke-ScriptAnalyzer" )
367
- . AddParameter ( "ScriptDefinition" , file . Contents ) ;
371
+ settingParameter = "Settings" ;
372
+ settingArgument = settings ;
373
+ }
374
+ else
375
+ {
376
+ settingParameter = "IncludeRule" ;
377
+ settingArgument = rules ;
378
+ }
368
379
369
- // Use a settings file if one is provided, otherwise use the default rule list.
370
- if ( settings != null )
371
- {
372
- powerShell . AddParameter ( "Settings" , settings ) ;
373
- }
374
- else
375
- {
376
- powerShell . AddParameter ( "IncludeRule" , rules ) ;
377
- }
378
380
379
- diagnosticRecords = powerShell . Invoke ( ) ;
380
- }
381
- }
381
+ diagnosticRecords = await InvokePowerShellAsync (
382
+ "Invoke-ScriptAnalyzer" ,
383
+ new Dictionary < string , object >
384
+ {
385
+ { "ScriptDefinition" , file . Contents } ,
386
+ { settingParameter , settingArgument }
387
+ } ) ;
382
388
}
383
389
384
390
Logger . Write (
@@ -388,6 +394,35 @@ private IEnumerable<PSObject> GetDiagnosticRecords<TSettings>(
388
394
return diagnosticRecords ;
389
395
}
390
396
397
+ private async Task < IEnumerable < PSObject > > InvokePowerShellAsync ( string command , IDictionary < string , object > paramArgMap )
398
+ {
399
+ var task = Task . Run ( ( ) =>
400
+ {
401
+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
402
+ {
403
+ powerShell . Runspace = this . analysisRunspace ;
404
+ powerShell . AddCommand ( command ) ;
405
+ foreach ( var kvp in paramArgMap )
406
+ {
407
+ powerShell . AddParameter ( kvp . Key , kvp . Value ) ;
408
+ }
409
+
410
+ var powerShellCommandResult = powerShell . BeginInvoke ( ) ;
411
+ var result = powerShell . EndInvoke ( powerShellCommandResult ) ;
412
+
413
+ if ( result == null )
414
+ {
415
+ return Enumerable . Empty < PSObject > ( ) ;
416
+ }
417
+
418
+ return result ;
419
+ }
420
+ } ) ;
421
+
422
+ await task ;
423
+ return task . Result ;
424
+ }
425
+
391
426
#endregion //private methods
392
427
}
393
428
}
0 commit comments