Powershell: pipe input to command executed via call operator

Question:

I need to execute a batch file dynamically via the call operator (&). The batch file takes arguments, but I also need to pass in some input on stdin. I thought this would work:

However, the "y" is not passed through in stdin. If I execute the batch file like this:

Then everything works as expected. But I cannot hard-code the path.

How can I execute a dynamically-determined batch file that takes arguments and pass through some standard input as well?

Update

In an effort to reproduce this, I did the following:

script.bat:

script2.bat:

Powershell session:

So it works as expected here. However, when I use it with the actual batch file (android.bat, part of the Android SDK), the input is not passed through to java.exe.

Here is the contents of android.bat:

The execution of the java app is prompting for license acceptance, which is why I want to pipe "y" into it. If I pipe directly it works, but if I pipe via Powershell’s call operator it does not.

This works:

This does not:

Can anyone enlighten me?

Answer:

I believe that using & to invoke an executable requires that the parameters are a String Array rather than just a String. I think PowerShell is getting confused on how to handle that input. So, per your example, something like this should pass inputs and arguments correctly:

results in the output

Source:

Powershell: pipe input to command executed via call operator by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply