1

I would like to know how to have one PowerShell instance dynamically control another PowerShell instance:

  1. Open a PowerShell instance (ogPS .. going forward) and execute a PowerShell script
  2. From in the ogPS script, start another PowerShell instance (newPS .. going forward)
  3. Monitor in ogPS the StdOut & ErrOut of the newPS, allowing ogPS to "watch" the status of commands
  4. Drive newPS by sending/streaming new commands to the StdIn of newPS, by responding to when newPS has returned a prompt
  5. Control newPS by perhaps "breaking" an operation that is taking too long to respond
  6. ogPS should be able to drive the newPS to gracefully exit by sending exit to newPS's StdIn

... ideally this would all be async / event driven (but I'll take a polling solution too).

This answer https://stackoverflow.com/a/9620390/1327508 to a similar question seems to be able to open a new instance and statically run some command(s); but it's not what I'm looking for.

Similarly, this answer https://stackoverflow.com/a/52807020/1327508 opens newPS from ogPS, but that's only a small part of the overall question.

Thanks in advance!

George 2.0 Hope
  • 583
  • 1
  • 6
  • 21
  • System.IO.Pipes contains some classes for this. Basically it comes down to creating a PipeServerStream and a PipeClientStream and sending data between them. As an alternative you can always use a socket connection on 127.0.0.1 to communicate between both processes – bluuf Jan 15 '21 at 11:51
  • @bluuf I'll have to do some further research on System.IO.Pipes to see if I can accomplish what I'm looking for, thanks for the suggestion. – George 2.0 Hope Jan 15 '21 at 17:17

1 Answers1

0

You can use Get-PSHostProcessInfo to see what PS instances are running, and use Enter-PSHostProcess to enter that window.

or, if you trying to work with a runspace with a debugger attached you may be able to use the following:

get-runspace | Where-object {_.Debugger.InBreakpoint -eq $true} | Debug-Runspace

pardon the messiness, im on my way out of work.

Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • I'll have to do some further research on your suggestions to see if I can accomplish what I'm looking for, thanks for the suggestion. – George 2.0 Hope Jan 15 '21 at 17:18