@@ -102,7 +102,10 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
102102 try
103103 {
104104 this . SettingsPath = settingsPath ;
105+
106+ scriptAnalyzerModuleInfo = FindPSScriptAnalyzerModule ( ) ;
105107 var sessionState = InitialSessionState . CreateDefault2 ( ) ;
108+ sessionState . ImportPSModulesFromPath ( scriptAnalyzerModuleInfo . ModuleBase ) ;
106109
107110 // runspacepool takes care of queuing commands for us so we do not
108111 // need to worry about executing concurrent commands
@@ -115,7 +118,6 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
115118 this . analysisRunspacePool . Open ( ) ;
116119
117120 ActiveRules = IncludedRules . ToArray ( ) ;
118- InitializePSScriptAnalyzer ( ) ;
119121 }
120122 catch ( Exception e )
121123 {
@@ -228,12 +230,10 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
228230 }
229231 }
230232
231- private void FindPSScriptAnalyzer ( )
233+ private static PSModuleInfo FindPSScriptAnalyzerModule ( )
232234 {
233235 using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
234236 {
235- ps . RunspacePool = this . analysisRunspacePool ;
236-
237237 ps . AddCommand ( "Get-Module" )
238238 . AddParameter ( "ListAvailable" )
239239 . AddParameter ( "Name" , "PSScriptAnalyzer" ) ;
@@ -245,60 +245,24 @@ private void FindPSScriptAnalyzer()
245245 ps . AddCommand ( "Select-Object" )
246246 . AddParameter ( "First" , 1 ) ;
247247
248- var modules = ps . Invoke ( ) ;
249-
250- var psModule = modules == null ? null : modules . FirstOrDefault ( ) ;
251- if ( psModule != null )
248+ var modules = ps . Invoke < PSModuleInfo > ( ) ;
249+ var scriptAnalyzerModuleInfo = modules == null ? null : modules . FirstOrDefault ( ) ;
250+ if ( scriptAnalyzerModuleInfo != null )
252251 {
253- scriptAnalyzerModuleInfo = psModule . ImmediateBaseObject as PSModuleInfo ;
254252 Logger . Write (
255253 LogLevel . Normal ,
256254 string . Format (
257255 "PSScriptAnalyzer found at {0}" ,
258256 scriptAnalyzerModuleInfo . Path ) ) ;
259- }
260- else
261- {
262- Logger . Write (
263- LogLevel . Normal ,
264- "PSScriptAnalyzer module was not found." ) ;
265- }
266- }
267- }
268-
269- private async Task < bool > ImportPSScriptAnalyzerAsync ( )
270- {
271- if ( scriptAnalyzerModuleInfo != null )
272- {
273- var module =
274- await InvokePowerShellAsync (
275- "Import-Module" ,
276- new Dictionary < string , object >
277- {
278- { "ModuleInfo" , scriptAnalyzerModuleInfo } ,
279- { "PassThru" , true } ,
280- } ) ;
281-
282- if ( module . Count ( ) == 0 )
283- {
284- this . scriptAnalyzerModuleInfo = null ;
285- Logger . Write ( LogLevel . Warning ,
286- String . Format ( "Cannot Import PSScriptAnalyzer: {0}" ) ) ;
287257
288- return false ;
258+ return scriptAnalyzerModuleInfo ;
289259 }
290- else
291- {
292- Logger . Write ( LogLevel . Normal ,
293- String . Format (
294- "Successfully imported PSScriptAnalyzer {0}" ,
295- scriptAnalyzerModuleInfo . Version ) ) ;
296260
297- return true ;
298- }
261+ Logger . Write (
262+ LogLevel . Normal ,
263+ "PSScriptAnalyzer module was not found." ) ;
264+ return null ;
299265 }
300-
301- return false ;
302266 }
303267
304268 private void EnumeratePSScriptAnalyzerRules ( )
@@ -317,23 +281,6 @@ private void EnumeratePSScriptAnalyzerRules()
317281 }
318282 }
319283
320- private void InitializePSScriptAnalyzer ( )
321- {
322- FindPSScriptAnalyzer ( ) ;
323-
324- List < Task > importTasks = new List < Task > ( ) ;
325- for ( int i = 0 ; i < NumRunspaces ; i ++ )
326- {
327- importTasks . Add (
328- ImportPSScriptAnalyzerAsync ( ) ) ;
329- }
330-
331- // Wait for the import requests to complete or fail
332- Task . WaitAll ( importTasks . ToArray ( ) ) ;
333-
334- EnumeratePSScriptAnalyzerRules ( ) ;
335- }
336-
337284 private async Task < PSObject [ ] > GetDiagnosticRecordsAsync < TSettings > (
338285 ScriptFile file ,
339286 string [ ] rules ,
@@ -399,7 +346,7 @@ private async Task<PSObject[]> InvokePowerShellAsync(string command, IDictionary
399346 return new PSObject [ 0 ] ;
400347 }
401348
402- return result . ToArray ( ) ; ;
349+ return result . ToArray ( ) ; ;
403350 }
404351 }
405352
0 commit comments