Skip to content

Commit 0e5d0dd

Browse files
author
Kapil Borle
committed
Use only method to invoke PS commands
1 parent 3a444ce commit 0e5d0dd

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,10 @@ public IEnumerable<string> GetPSScriptAnalyzerRules()
158158
List<string> ruleNames = new List<string>();
159159
if (scriptAnalyzerModuleInfo != null)
160160
{
161-
lock (runspaceLock)
161+
var ruleObjects = InvokePowerShell("Get-ScriptAnalyzerRule", new Dictionary<string, object>());
162+
foreach (var rule in ruleObjects)
162163
{
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);
172165
}
173166
}
174167

@@ -250,64 +243,53 @@ private ScriptFileMarker[] GetSemanticMarkers<TSettings>(
250243

251244
private void FindPSScriptAnalyzer()
252245
{
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>
256249
{
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.");
281268
}
282269
}
283270

284271
private void ImportPSScriptAnalyzer()
285272
{
286273
if (scriptAnalyzerModuleInfo != null)
287274
{
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>
291278
{
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+
});
298282

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"));
311293
}
312294
}
313295
}
@@ -345,7 +327,7 @@ private IEnumerable<PSObject> GetDiagnosticRecords(ScriptFile file)
345327
private IEnumerable<PSObject> GetDiagnosticRecords<TSettings>(
346328
ScriptFile file,
347329
string[] rules,
348-
TSettings settings) where TSettings: class
330+
TSettings settings) where TSettings : class
349331
{
350332
var task = GetDiagnosticRecordsAsync(file, rules, settings);
351333
task.Wait();
@@ -394,6 +376,13 @@ private async Task<IEnumerable<PSObject>> GetDiagnosticRecordsAsync<TSettings>(
394376
return diagnosticRecords;
395377
}
396378

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+
397386
private async Task<IEnumerable<PSObject>> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap)
398387
{
399388
var task = Task.Run(() =>

0 commit comments

Comments
 (0)