-
Notifications
You must be signed in to change notification settings - Fork 240
NamedPipeConnectionInfo <= Enter-PSHostProcess #881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NamedPipeConnectionInfo <= Enter-PSHostProcess #881
Conversation
|
@SeeminglyScience let me know if you see anything wrong with this in the scope of the threading model... I think it will be fine since it's a totally separate runspace... |
| else | ||
| { | ||
| var psCommand = new PSCommand().AddCommand("Get-Runspace"); | ||
| StringBuilder sb = new StringBuilder(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use var here - channeling my inner @rjmholt. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got me!
| { | ||
| foreach (var p in runspaces) | ||
| // Create a remote runspace that we will invoke Get-Runspace in | ||
| using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work with all supported versions of PowerShell?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch... this use to be a runtime check using Enter-PSHostProcess but now it's an assembly load check...
This will work in PowerShell 5.1+ so it's not a problem in this PR.
But when I backport I need to ifdef this properly...
SeeminglyScience
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM :)
Just some optional nits
| rs.Open(); | ||
| ps.Runspace = rs; | ||
| // returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. | ||
| runspaces = ps.AddCommand("Get-Runspace").Invoke<PSObject>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| runspaces = ps.AddCommand("Get-Runspace").Invoke<PSObject>(); | |
| runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke<PSObject>(); |
| } | ||
| else | ||
| { | ||
| var psCommand = new PSCommand().AddCommand("Get-Runspace"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var psCommand = new PSCommand().AddCommand("Get-Runspace"); | |
| var psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace"); |
| if (int.TryParse(processId, out int pid)) | ||
| { | ||
| foreach (var p in runspaces) | ||
| // Create a remote runspace that we will invoke Get-Runspace in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // Create a remote runspace that we will invoke Get-Runspace in | |
| // Create a remote runspace that we will invoke Get-Runspace in. |
| }); | ||
| rs.Open(); | ||
| ps.Runspace = rs; | ||
| // returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. | |
| // Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. |
| IEnumerable<Runspace> runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<Runspace>(psCommand, sb); | ||
| if (runspaces != null) | ||
| // If the processId is a valid int, we need to run Get-Runspace within that process | ||
| // otherwise just use the current runspace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // otherwise just use the current runspace | |
| // otherwise just use the current runspace. |
| await editorSession.PowerShellContext.ExecuteCommandAsync(exitCommand); | ||
| if (runspaces != null) | ||
| { | ||
| foreach (dynamic p in runspaces) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a more descriptive variable name like runspace or even pso.
Edit: didn't realize this was existing code, but maybe still worth fixing.
cc @adamdriscoll
Before, we relied on Enter-PSHostProcess... I noticed that this wasn't working and instead the GeRunspace function would return results from the current runspace instead.
This switches to using
NamedPipeConnectionInfoAPI way of invoking something on a remote runspace.