|
16 | 16 | using System.Diagnostics;
|
17 | 17 | using System.IO;
|
18 | 18 | using System.Linq;
|
19 |
| -using System.Management.Automation; |
20 | 19 | using System.Management.Automation.Language;
|
21 | 20 | using System.Management.Automation.Runspaces;
|
22 | 21 | using System.Text;
|
|
28 | 27 |
|
29 | 28 | namespace Microsoft.PowerShell.EditorServices.Protocol.Server
|
30 | 29 | {
|
| 30 | + using System.Management.Automation; |
| 31 | + |
31 | 32 | public class LanguageServer
|
32 | 33 | {
|
33 | 34 | private static CancellationTokenSource s_existingRequestCancellation;
|
@@ -1225,44 +1226,50 @@ protected async Task HandleGetRunspaceRequestAsync(
|
1225 | 1226 | string processId,
|
1226 | 1227 | RequestContext<GetRunspaceResponse[]> requestContext)
|
1227 | 1228 | {
|
1228 |
| - var runspaceResponses = new List<GetRunspaceResponse>(); |
| 1229 | + IEnumerable<PSObject> runspaces = null; |
1229 | 1230 |
|
1230 | 1231 | if (this.editorSession.PowerShellContext.LocalPowerShellVersion.Version.Major >= 5)
|
1231 | 1232 | {
|
1232 | 1233 | if (processId == null) {
|
1233 | 1234 | processId = "current";
|
1234 | 1235 | }
|
1235 | 1236 |
|
1236 |
| - var isNotCurrentProcess = processId != null && processId != "current"; |
1237 |
| - |
1238 |
| - var psCommand = new PSCommand(); |
1239 |
| - |
1240 |
| - if (isNotCurrentProcess) { |
1241 |
| - psCommand.AddCommand("Enter-PSHostProcess").AddParameter("Id", processId).AddStatement(); |
1242 |
| - } |
1243 |
| - |
1244 |
| - psCommand.AddCommand("Get-Runspace"); |
1245 |
| - |
1246 |
| - StringBuilder sb = new StringBuilder(); |
1247 |
| - IEnumerable<Runspace> runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<Runspace>(psCommand, sb); |
1248 |
| - if (runspaces != null) |
| 1237 | + // If the processId is a valid int, we need to run Get-Runspace within that process |
| 1238 | + // otherwise just use the current runspace. |
| 1239 | + if (int.TryParse(processId, out int pid)) |
1249 | 1240 | {
|
1250 |
| - foreach (var p in runspaces) |
| 1241 | + // Create a remote runspace that we will invoke Get-Runspace in. |
| 1242 | + using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid))) |
| 1243 | + using(var ps = PowerShell.Create()) |
1251 | 1244 | {
|
1252 |
| - runspaceResponses.Add( |
1253 |
| - new GetRunspaceResponse |
1254 |
| - { |
1255 |
| - Id = p.Id, |
1256 |
| - Name = p.Name, |
1257 |
| - Availability = p.RunspaceAvailability.ToString() |
1258 |
| - }); |
| 1245 | + rs.Open(); |
| 1246 | + ps.Runspace = rs; |
| 1247 | + // Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
| 1248 | + runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke<PSObject>(); |
1259 | 1249 | }
|
1260 | 1250 | }
|
| 1251 | + else |
| 1252 | + { |
| 1253 | + var psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace"); |
| 1254 | + var sb = new StringBuilder(); |
| 1255 | + // returns (not deserialized) Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
| 1256 | + runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<PSObject>(psCommand, sb); |
| 1257 | + } |
| 1258 | + } |
| 1259 | + |
| 1260 | + var runspaceResponses = new List<GetRunspaceResponse>(); |
1261 | 1261 |
|
1262 |
| - if (isNotCurrentProcess) { |
1263 |
| - var exitCommand = new PSCommand(); |
1264 |
| - exitCommand.AddCommand("Exit-PSHostProcess"); |
1265 |
| - await editorSession.PowerShellContext.ExecuteCommandAsync(exitCommand); |
| 1262 | + if (runspaces != null) |
| 1263 | + { |
| 1264 | + foreach (dynamic runspace in runspaces) |
| 1265 | + { |
| 1266 | + runspaceResponses.Add( |
| 1267 | + new GetRunspaceResponse |
| 1268 | + { |
| 1269 | + Id = runspace.Id, |
| 1270 | + Name = runspace.Name, |
| 1271 | + Availability = runspace.RunspaceAvailability.ToString() |
| 1272 | + }); |
1266 | 1273 | }
|
1267 | 1274 | }
|
1268 | 1275 |
|
|
0 commit comments