1111using System . Linq ;
1212using System . Management . Automation ;
1313using System . Management . Automation . Language ;
14+ using System . Management . Automation . Runspaces ;
1415
1516namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
1617{
@@ -29,6 +30,7 @@ public class Helper
2930 private PSVersionTable psVersionTable ;
3031
3132 private readonly Lazy < CommandInfoCache > _commandInfoCacheLazy ;
33+ private readonly RunspacePool _runSpacePool ;
3234
3335 #endregion
3436
@@ -113,7 +115,10 @@ internal set
113115 /// </summary>
114116 private Helper ( )
115117 {
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 ) ) ;
117122 }
118123
119124 /// <summary>
@@ -299,11 +304,12 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
299304 Collection < PSObject > psObj = null ;
300305 using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
301306 {
307+ ps . RunspacePool = _runSpacePool ;
308+ ps . AddCommand ( "Test-ModuleManifest" ) ;
309+ ps . AddParameter ( "Path" , filePath ) ;
310+ ps . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
302311 try
303312 {
304- ps . AddCommand ( "Test-ModuleManifest" ) ;
305- ps . AddParameter ( "Path" , filePath ) ;
306- ps . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
307313 psObj = ps . Invoke ( ) ;
308314 }
309315 catch ( CmdletInvocationException e )
@@ -653,31 +659,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona
653659 return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0 ;
654660 }
655661
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-
681662 /// <summary>
682663 /// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
683664 /// 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