@@ -102,7 +102,10 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
102
102
try
103
103
{
104
104
this . SettingsPath = settingsPath ;
105
+
106
+ scriptAnalyzerModuleInfo = FindPSScriptAnalyzerModule ( ) ;
105
107
var sessionState = InitialSessionState . CreateDefault2 ( ) ;
108
+ sessionState . ImportPSModulesFromPath ( scriptAnalyzerModuleInfo . ModuleBase ) ;
106
109
107
110
// runspacepool takes care of queuing commands for us so we do not
108
111
// need to worry about executing concurrent commands
@@ -115,7 +118,6 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
115
118
this . analysisRunspacePool . Open ( ) ;
116
119
117
120
ActiveRules = IncludedRules . ToArray ( ) ;
118
- InitializePSScriptAnalyzer ( ) ;
119
121
}
120
122
catch ( Exception e )
121
123
{
@@ -228,12 +230,10 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
228
230
}
229
231
}
230
232
231
- private void FindPSScriptAnalyzer ( )
233
+ private static PSModuleInfo FindPSScriptAnalyzerModule ( )
232
234
{
233
235
using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
234
236
{
235
- ps . RunspacePool = this . analysisRunspacePool ;
236
-
237
237
ps . AddCommand ( "Get-Module" )
238
238
. AddParameter ( "ListAvailable" )
239
239
. AddParameter ( "Name" , "PSScriptAnalyzer" ) ;
@@ -245,60 +245,24 @@ private void FindPSScriptAnalyzer()
245
245
ps . AddCommand ( "Select-Object" )
246
246
. AddParameter ( "First" , 1 ) ;
247
247
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 )
252
251
{
253
- scriptAnalyzerModuleInfo = psModule . ImmediateBaseObject as PSModuleInfo ;
254
252
Logger . Write (
255
253
LogLevel . Normal ,
256
254
string . Format (
257
255
"PSScriptAnalyzer found at {0}" ,
258
256
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}" ) ) ;
287
257
288
- return false ;
258
+ return scriptAnalyzerModuleInfo ;
289
259
}
290
- else
291
- {
292
- Logger . Write ( LogLevel . Normal ,
293
- String . Format (
294
- "Successfully imported PSScriptAnalyzer {0}" ,
295
- scriptAnalyzerModuleInfo . Version ) ) ;
296
260
297
- return true ;
298
- }
261
+ Logger . Write (
262
+ LogLevel . Normal ,
263
+ "PSScriptAnalyzer module was not found." ) ;
264
+ return null ;
299
265
}
300
-
301
- return false ;
302
266
}
303
267
304
268
private void EnumeratePSScriptAnalyzerRules ( )
@@ -317,23 +281,6 @@ private void EnumeratePSScriptAnalyzerRules()
317
281
}
318
282
}
319
283
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
-
337
284
private async Task < PSObject [ ] > GetDiagnosticRecordsAsync < TSettings > (
338
285
ScriptFile file ,
339
286
string [ ] rules ,
@@ -399,7 +346,7 @@ private async Task<PSObject[]> InvokePowerShellAsync(string command, IDictionary
399
346
return new PSObject [ 0 ] ;
400
347
}
401
348
402
- return result . ToArray ( ) ; ;
349
+ return result . ToArray ( ) ; ;
403
350
}
404
351
}
405
352
0 commit comments