I'm trying to use exec to interface with the netsh wlan commands on Windows, and I've been having no trouble up until for some reason I get an exit code of 1 for a very specific command and circumstance. Most commands work fine with no issue, but when trying to call netsh wlan show profiles which lists all wireless network profiles for the system, I get an error ONLY when there are no profiles at all:
Error: Command failed: netsh wlan show profiles
Any profiles exist and the command returns fine. It only takes one, as in the example output below.
Output from netsh wlan show profiles w/o profiles: (error)
Profiles on interface Wi=Fi:
Group policy profiles (read only)
---------------------------------
<None>
User profiles
-------------
<None>
Output from netsh wlan show profiles w/ profiles: (no error)
Profiles on interface Wi=Fi:
Group policy profiles (read only)
---------------------------------
<None>
User profiles
-------------
All User Profile : MyWiFiNetwork
The "error" doesn't seem to prevent the command from executing either. stdout is passed properly and I can proceed if I ignore the error, but then I won't catch any actual errors.
I tried replacing exec with execSync since I couldn't find anything trying to debug the async version and still got the same error. I ended up finding that your binding returns a status of 1 for some reason.
var result = spawn_sync.spawn(options);
The result object returned:
{
output: Array[3] [null, Uint8Array[162], Uint8Array[0] ],
pid: /*pid*/,
signal: null,
status: 1
}
It also has a prototype but this looks like a standard Object prototype.
I have no idea how to try and debug this binding, so that is about as deep of information as I can give on that front, but I have replicated this on multiple machines in isolated code (below).
const execSync = require('child_process').execSync;
try {
let out = execSync('netsh wlan show profiles').toString();
console.log(out);
} catch(err) {
console.error(err);
}
Note: also tried const cp = require('child_process'); followed by cp.execSync() later but this had no effect on behavior.
execSync throws when the command has non-zero exit code. You can access child output from the exception object.
If you want to get net wlan show profiles output no matter if it fails or not, you can use either asynchronous version of exec. The callback will have stdout set.
Or you can catch exception and get stdout from there. Something like:
let nestsh_output;
try {
netsh_output = execSync('netsh wlan show profiles');
} catch (ex) {
netsh_output = ex.stdout;
}
That said, I don't see any issue with Node.js here, so I will close this. If you think that there is a bug here feel free to reopen the issue. If you have more questions about how to do stuff in Node.js, check out https://github.com/nodejs/help.
There should be a way to configure child_process to not consider certain codes as errors.
PRs are always welcomed
Most helpful comment
execSyncthrows when the command has non-zero exit code. You can access child output from the exception object.If you want to get
net wlan show profilesoutput no matter if it fails or not, you can use either asynchronous version ofexec. The callback will havestdoutset.Or you can catch exception and get
stdoutfrom there. Something like:That said, I don't see any issue with Node.js here, so I will close this. If you think that there is a bug here feel free to reopen the issue. If you have more questions about how to do stuff in Node.js, check out https://github.com/nodejs/help.