0

I've looked at the PowerShell documentation for how to start a subprocess and create pipes for stdin/stdout.

However, I cannot seem to find any information on the subject.

What I want to achieve is somewhat similar to the C++ code at MSDN:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

Does PowerShell provide equivalent functionality as a scripting language?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • 2
    What is your end goal? What problem are you trying to solve by doing this? In any case you might want to look at this related question: [Can I send some text to the STDIN of an active process under Windows](https://stackoverflow.com/questions/16098366/can-i-send-some-text-to-the-stdin-of-an-active-process-under-windows) – boxdog May 31 '18 at 11:17

1 Answers1

0

Yes. Here an example of a subprocess, the standard output gets redirected to a file. You have to specify in detail, what you want to do.

scriptBlock = {

    "Hello World" | Out-File -FilePath 'd:\jobTest.txt'

 }

Start-Job -ScriptBlock $scriptBlock
f6a4
  • 1,684
  • 1
  • 10
  • 13
  • Thank you - I want to implement a netcat-like client in PowerShell. In particular, I want to: 1) Connect a TCP socket, 2) Start a CMD.exe subprocess, 3) Create a pipe between socket and CMD.exe. Any ideas? Is it possible? – Shuzheng Jun 03 '18 at 11:11
  • Yes. I would do this using runspaces and a synchronized hash for data exchange between job 1 (socket) and job 2 (cmd). – f6a4 Jun 03 '18 at 13:43
  • Is it possible to give a small example, or would it fill too much? – Shuzheng Jun 03 '18 at 15:20
  • Okay, I see how runspaces work. But it seems that I can only retrieve output after the `BeginInvoke()` completes? – Shuzheng Jun 03 '18 at 16:03
  • This is a good excample for introduction: https://learn-powershell.net/2013/04/19/sharing-variables-and-live-objects-between-powershell-runspaces/ – f6a4 Jun 03 '18 at 19:12