|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Management.Automation; |
| 3 | +using System.Management.Automation.Runspaces; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | +namespace PowerShellEditorServices.Engine.Services.Handlers |
| 9 | +{ |
| 10 | + public class PSHostProcessAndRunspaceHandlers : IGetPSHostProcessesHandler, IGetRunspaceHandler |
| 11 | + { |
| 12 | + private readonly ILogger<GetVersionHandler> _logger; |
| 13 | + |
| 14 | + public PSHostProcessAndRunspaceHandlers(ILoggerFactory factory) |
| 15 | + { |
| 16 | + _logger = factory.CreateLogger<GetVersionHandler>(); |
| 17 | + } |
| 18 | + |
| 19 | + public Task<PSHostProcessResponse[]> Handle(GetPSHostProcesssesParams request, CancellationToken cancellationToken) |
| 20 | + { |
| 21 | + var psHostProcesses = new List<PSHostProcessResponse>(); |
| 22 | + |
| 23 | + int processId = System.Diagnostics.Process.GetCurrentProcess().Id; |
| 24 | + |
| 25 | + using (var pwsh = PowerShell.Create()) |
| 26 | + { |
| 27 | + pwsh.AddCommand("Get-PSHostProcessInfo") |
| 28 | + .AddCommand("Where-Object") |
| 29 | + .AddParameter("Property", "ProcessId") |
| 30 | + .AddParameter("NE") |
| 31 | + .AddParameter("Value", processId.ToString()); |
| 32 | + |
| 33 | + var processes = pwsh.Invoke<PSObject>(); |
| 34 | + |
| 35 | + if (processes != null) |
| 36 | + { |
| 37 | + foreach (dynamic p in processes) |
| 38 | + { |
| 39 | + psHostProcesses.Add( |
| 40 | + new PSHostProcessResponse |
| 41 | + { |
| 42 | + ProcessName = p.ProcessName, |
| 43 | + ProcessId = p.ProcessId, |
| 44 | + AppDomainName = p.AppDomainName, |
| 45 | + MainWindowTitle = p.MainWindowTitle |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return Task.FromResult(psHostProcesses.ToArray()); |
| 52 | + } |
| 53 | + |
| 54 | + public Task<RunspaceResponse[]> Handle(GetRunspaceParams request, CancellationToken cancellationToken) |
| 55 | + { |
| 56 | + IEnumerable<PSObject> runspaces = null; |
| 57 | + |
| 58 | + if (request.ProcessId == null) { |
| 59 | + request.ProcessId = "current"; |
| 60 | + } |
| 61 | + |
| 62 | + // If the processId is a valid int, we need to run Get-Runspace within that process |
| 63 | + // otherwise just use the current runspace. |
| 64 | + if (int.TryParse(request.ProcessId, out int pid)) |
| 65 | + { |
| 66 | + // Create a remote runspace that we will invoke Get-Runspace in. |
| 67 | + using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid))) |
| 68 | + using(var ps = PowerShell.Create()) |
| 69 | + { |
| 70 | + rs.Open(); |
| 71 | + ps.Runspace = rs; |
| 72 | + // Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
| 73 | + runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke<PSObject>(); |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + // TODO: Bring back |
| 79 | + // var psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace"); |
| 80 | + // var sb = new StringBuilder(); |
| 81 | + // // returns (not deserialized) Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
| 82 | + // runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<PSObject>(psCommand, sb); |
| 83 | + } |
| 84 | + |
| 85 | + var runspaceResponses = new List<RunspaceResponse>(); |
| 86 | + |
| 87 | + if (runspaces != null) |
| 88 | + { |
| 89 | + foreach (dynamic runspace in runspaces) |
| 90 | + { |
| 91 | + runspaceResponses.Add( |
| 92 | + new RunspaceResponse |
| 93 | + { |
| 94 | + Id = runspace.Id, |
| 95 | + Name = runspace.Name, |
| 96 | + Availability = runspace.RunspaceAvailability.ToString() |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return Task.FromResult(runspaceResponses.ToArray()); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments