11
11
using System . Linq ;
12
12
using System . Management . Automation ;
13
13
using System . Management . Automation . Language ;
14
+ using System . Management . Automation . Runspaces ;
14
15
15
16
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
16
17
{
@@ -29,6 +30,7 @@ public class Helper
29
30
private PSVersionTable psVersionTable ;
30
31
31
32
private readonly Lazy < CommandInfoCache > _commandInfoCacheLazy ;
33
+ private readonly RunspacePool _runSpacePool ;
32
34
33
35
#endregion
34
36
@@ -113,7 +115,10 @@ internal set
113
115
/// </summary>
114
116
private Helper ( )
115
117
{
116
- _commandInfoCacheLazy = new Lazy < CommandInfoCache > ( ( ) => new CommandInfoCache ( pssaHelperInstance : this ) ) ;
118
+ // There are 5 rules that use the CommandInfo cache and each rule does not request more than one concurrent command info request
119
+ _runSpacePool = RunspaceFactory . CreateRunspacePool ( 1 , 6 ) ;
120
+ _runSpacePool . Open ( ) ;
121
+ _commandInfoCacheLazy = new Lazy < CommandInfoCache > ( ( ) => new CommandInfoCache ( pssaHelperInstance : this , runspacePool : _runSpacePool ) ) ;
117
122
}
118
123
119
124
/// <summary>
@@ -299,11 +304,12 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
299
304
Collection < PSObject > psObj = null ;
300
305
using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
301
306
{
307
+ ps . RunspacePool = _runSpacePool ;
308
+ ps . AddCommand ( "Test-ModuleManifest" ) ;
309
+ ps . AddParameter ( "Path" , filePath ) ;
310
+ ps . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
302
311
try
303
312
{
304
- ps . AddCommand ( "Test-ModuleManifest" ) ;
305
- ps . AddParameter ( "Path" , filePath ) ;
306
- ps . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
307
313
psObj = ps . Invoke ( ) ;
308
314
}
309
315
catch ( CmdletInvocationException e )
@@ -653,31 +659,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona
653
659
return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0 ;
654
660
}
655
661
656
-
657
- /// <summary>
658
- /// Get a CommandInfo object of the given command name
659
- /// </summary>
660
- /// <returns>Returns null if command does not exists</returns>
661
- private CommandInfo GetCommandInfoInternal ( string cmdName , CommandTypes ? commandType )
662
- {
663
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
664
- {
665
- var psCommand = ps . AddCommand ( "Get-Command" )
666
- . AddParameter ( "Name" , cmdName )
667
- . AddParameter ( "ErrorAction" , "SilentlyContinue" ) ;
668
-
669
- if ( commandType != null )
670
- {
671
- psCommand . AddParameter ( "CommandType" , commandType ) ;
672
- }
673
-
674
- var commandInfo = psCommand . Invoke < CommandInfo > ( )
675
- . FirstOrDefault ( ) ;
676
-
677
- return commandInfo ;
678
- }
679
- }
680
-
681
662
/// <summary>
682
663
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
683
664
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.
0 commit comments