Skip to content

Instantly share code, notes, and snippets.

@Phineas
Created November 28, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phineas/6a30ce239703f7600ca7ea81d1f98a97 to your computer and use it in GitHub Desktop.
Save Phineas/6a30ce239703f7600ca7ea81d1f98a97 to your computer and use it in GitHub Desktop.
var spawn = require('child_process').spawn;
var xml2js = require('xml2js');
/**
* Returns all the system processes
* @param callback {function(err, list)} Called with the list of all processes.
* @remarks Runs only on Windows (uses WMI)
*/
module.exports = function (callback) {
if (!callback) callback = function (err, list) { };
var options = [
'process',
'list',
'/format:rawxml'
];
// Spawn process, since output from exec can be too big for buffer size supported.
var p = spawn('wmic', options);
var xml = '';
p.stdout.on('data', function (data) {
xml = xml + data.toString();
});
p.stderr.on('data', function (data) {
console.error(data);
});
p.on('exit', function () {
parser = new xml2js.Parser();
parser.parseString(xml, function (err, result) {
var output = {};
result.RESULTS.CIM.INSTANCE.forEach(function (p) {
var entry = {};
p.PROPERTY.forEach(function (v) {
entry[v['@'].NAME] = v.VALUE;
});
var e = {
pid: entry.Handle,
desc: entry.Description,
cmd: entry.CommandLine,
prog: entry.ExecutablePath
};
if (!e.cmd) delete e.cmd;
if (!e.prog) delete e.prog;
if (e.pid) {
output[e.pid] = e;
}
});
callback(null, output);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment