I've having a problem with the Windows program WMIC.exe (used for querying Windows Management Instrumentation), from within NodeJS.
I can spawn a wmic.exe process, but it'll not output anything or accept any input. However, if I set the stdin to null it'll run correctly and I'll receive output.
var spawn = require('child_process').spawn;
var wmic = spawn('wmic', [], {stdio: ['ignore']});
wmic.stdout.on('data', function(data) {
console.log('Data received:' + data);
});
wmic.on('close', function(code) {
console.log('Spawned process ended with code: ' + code);
});
I want to use wmic.exe interactively, which it supports, keeping it open so I don't have to repeatedly start it for each WMI query. Others have had similar problems with wmic.exe, but they only wanted to capture output for a single query, so having a null stdin doesn't matter to them.
Update
If I do the following...
var spawn = require('child_process').spawn;
var wmic = spawn('wmic', []);
wmic.stdout.on('data', function(data) {
console.log('Data received:' + data);
});
wmic.stderr.on('data', function(data) {
console.log('Error! - ' + data);
});
wmic.on('close', function(code) {
console.log('Spawned process ended with code: ' + code);
});
wmic.stdin.end('cpu get caption /format:csv\n');
Then it'll actually respond with a result, prompt back ready for the next query but next ends the process and obviously I can't use .write() after .end(). Changing it to a .write() instead, process doesn't respond at all and I don't even get the prompt from stdout "wmic:root\cli>" that I receive when using .end().
Or
wmic.stdin.push(null);
wmic.stdin.write('cpu/n'); // Ctrl-Z aka Windows EOF control code
If I use the above, instead of the .end() call, it'll also work. But it seems that if I don't keep throwing constant data at it, the process ends.
Or even
wmic.stdin.write('cpu');
wmic.stdin.write('\x1a');
This works too, but yet again, the wmic.exe process decides to exit after the cpu results are returned. :/
Almost
var wmic = spawn('wmic.exe', [], {stdio: [process.stdin, 'pipe', 'pipe']});
This works. The process starts up correctly so I receive the prompt from wmic.exe through wmic.stdout.on('data', fn) and it stays open. However, I'm unable to send input to it via code but can type directly into the command prompt window. Anything I type is correctly executed and output via the node script. Again, weird that it works with this pipe but not any I set and useless to me as I want to send commands via code, not typing them.
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.