@@ -158,17 +158,10 @@ public IEnumerable<string> GetPSScriptAnalyzerRules()
158
158
List < string > ruleNames = new List < string > ( ) ;
159
159
if ( scriptAnalyzerModuleInfo != null )
160
160
{
161
- lock ( runspaceLock )
161
+ var ruleObjects = InvokePowerShell ( "Get-ScriptAnalyzerRule" , new Dictionary < string , object > ( ) ) ;
162
+ foreach ( var rule in ruleObjects )
162
163
{
163
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
164
- {
165
- ps . Runspace = this . analysisRunspace ;
166
- var ruleObjects = ps . AddCommand ( "Get-ScriptAnalyzerRule" ) . Invoke ( ) ;
167
- foreach ( var rule in ruleObjects )
168
- {
169
- ruleNames . Add ( ( string ) rule . Members [ "RuleName" ] . Value ) ;
170
- }
171
- }
164
+ ruleNames . Add ( ( string ) rule . Members [ "RuleName" ] . Value ) ;
172
165
}
173
166
}
174
167
@@ -250,64 +243,53 @@ private ScriptFileMarker[] GetSemanticMarkers<TSettings>(
250
243
251
244
private void FindPSScriptAnalyzer ( )
252
245
{
253
- lock ( runspaceLock )
254
- {
255
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
246
+ var modules = InvokePowerShell (
247
+ "Get-Module" ,
248
+ new Dictionary < string , object >
256
249
{
257
- ps . Runspace = this . analysisRunspace ;
258
-
259
- var modules = ps . AddCommand ( "Get-Module" )
260
- . AddParameter ( "List" )
261
- . AddParameter ( "Name" , "PSScriptAnalyzer" )
262
- . Invoke ( ) ;
263
-
264
- var psModule = modules == null ? null : modules . FirstOrDefault ( ) ;
265
- if ( psModule != null )
266
- {
267
- scriptAnalyzerModuleInfo = psModule . ImmediateBaseObject as PSModuleInfo ;
268
- Logger . Write (
269
- LogLevel . Normal ,
270
- string . Format (
271
- "PSScriptAnalyzer found at {0}" ,
272
- scriptAnalyzerModuleInfo . Path ) ) ;
273
- }
274
- else
275
- {
276
- Logger . Write (
277
- LogLevel . Normal ,
278
- "PSScriptAnalyzer module was not found." ) ;
279
- }
280
- }
250
+ { "ListAvailable" , true } ,
251
+ { "Name" , "PSScriptAnalyzer" }
252
+ } ) ;
253
+ var psModule = modules . Count ( ) == 0 ? null : modules . FirstOrDefault ( ) ;
254
+ if ( psModule != null )
255
+ {
256
+ scriptAnalyzerModuleInfo = psModule . ImmediateBaseObject as PSModuleInfo ;
257
+ Logger . Write (
258
+ LogLevel . Normal ,
259
+ string . Format (
260
+ "PSScriptAnalyzer found at {0}" ,
261
+ scriptAnalyzerModuleInfo . Path ) ) ;
262
+ }
263
+ else
264
+ {
265
+ Logger . Write (
266
+ LogLevel . Normal ,
267
+ "PSScriptAnalyzer module was not found." ) ;
281
268
}
282
269
}
283
270
284
271
private void ImportPSScriptAnalyzer ( )
285
272
{
286
273
if ( scriptAnalyzerModuleInfo != null )
287
274
{
288
- lock ( runspaceLock )
289
- {
290
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
275
+ var module = InvokePowerShell (
276
+ "Import-Module" ,
277
+ new Dictionary < string , object >
291
278
{
292
- ps . Runspace = this . analysisRunspace ;
293
-
294
- var module = ps . AddCommand ( "Import-Module" )
295
- . AddParameter ( "ModuleInfo" , scriptAnalyzerModuleInfo )
296
- . AddParameter ( "PassThru" )
297
- . Invoke ( ) ;
279
+ { "ModuleInfo" , scriptAnalyzerModuleInfo } ,
280
+ { "PassThru" , true } ,
281
+ } ) ;
298
282
299
- if ( module == null )
300
- {
301
- this . scriptAnalyzerModuleInfo = null ;
302
- Logger . Write ( LogLevel . Warning ,
303
- String . Format ( "Cannot Import PSScriptAnalyzer: {0}" ) ) ;
304
- }
305
- else
306
- {
307
- Logger . Write ( LogLevel . Normal ,
308
- String . Format ( "Successfully imported PSScriptAnalyzer" ) ) ;
309
- }
310
- }
283
+ if ( module . Count ( ) == 0 )
284
+ {
285
+ this . scriptAnalyzerModuleInfo = null ;
286
+ Logger . Write ( LogLevel . Warning ,
287
+ String . Format ( "Cannot Import PSScriptAnalyzer: {0}" ) ) ;
288
+ }
289
+ else
290
+ {
291
+ Logger . Write ( LogLevel . Normal ,
292
+ String . Format ( "Successfully imported PSScriptAnalyzer" ) ) ;
311
293
}
312
294
}
313
295
}
@@ -345,7 +327,7 @@ private IEnumerable<PSObject> GetDiagnosticRecords(ScriptFile file)
345
327
private IEnumerable < PSObject > GetDiagnosticRecords < TSettings > (
346
328
ScriptFile file ,
347
329
string [ ] rules ,
348
- TSettings settings ) where TSettings : class
330
+ TSettings settings ) where TSettings : class
349
331
{
350
332
var task = GetDiagnosticRecordsAsync ( file , rules , settings ) ;
351
333
task . Wait ( ) ;
@@ -394,6 +376,13 @@ private async Task<IEnumerable<PSObject>> GetDiagnosticRecordsAsync<TSettings>(
394
376
return diagnosticRecords ;
395
377
}
396
378
379
+ private IEnumerable < PSObject > InvokePowerShell ( string command , IDictionary < string , object > paramArgMap )
380
+ {
381
+ var task = InvokePowerShellAsync ( command , paramArgMap ) ;
382
+ task . Wait ( ) ;
383
+ return task . Result ;
384
+ }
385
+
397
386
private async Task < IEnumerable < PSObject > > InvokePowerShellAsync ( string command , IDictionary < string , object > paramArgMap )
398
387
{
399
388
var task = Task . Run ( ( ) =>
0 commit comments