You want to use wmic
interactively, but driven by input from an application rather than input typed at the console.
The normal way to achieve this for any application is to feed it input from a pipe. Conveniently, NodeJS appears to create a pipe by default if you don't specify any other stdio
option.
The problem is that wmic
behaves badly when it is receiving input from a pipe. It refuses to do anything until the input is complete; it then processes all the input and stops looking for any more. wmic
thinks that the input is complete when the pipe is closed or it receives a CTRL-Z.
Hence you cannot issue one command, read the output and then issue another command. wmic
doesn't write output until it thinks it has received all the commands, and then it won't accept any more.
You can illustrate this problem at the console. type con: | wmic
puts keyboard input through a pipe so wmic
goes into its broken mode. In the example below this is followed by a couple of commands. Note that wmic
does nothing until CTRL-Z
is entered.
C:\>type con: | wmic
cpu get name
useraccount list brief
^Z
wmic:root\cli>cpu get name
Name
Pentium(R) Dual-Core CPU T4500 @ 2.30GHz
wmic:root\cli>
"/?" for help, QUIT to Exit.
wmic:root\cli>useraccount list brief
AccountType Caption Domain FullName Name
512 frog\Administrator frog Administrator
wmic:root\cli>
"/?" for help, QUIT to Exit.
wmic:root\cli>
C:\>
You might think (looking at this example) that wmic
just has a large output buffer. However, if you turn on tracing (/TRACE:ON
as the first command) wmic
produces reams of output, but nothing before the input is complete.
A solution is to not redirect the standard input, like your final example, so the input is coming from the keyboard. (N.B. when a Windows console application receives input from the keyboard it is not coming through a pipe. Hence your final example is not using a pipe, which is why is behaves differently.)
You can then supply input to wmic
by simulating keystrokes. Answers to this question suggest a couple of methods: either using SendKeys
or by sending WM_KEYUP
messages. This is not a great solution.