wmic.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var os = require('os');
  3. var bin = require('./bin');
  4. /**
  5. * Gets the list of all the pids of the system through the wmic command.
  6. * @param {Function} callback(err, list)
  7. */
  8. function wmic(callback) {
  9. var args = ['PROCESS', 'get', 'ParentProcessId,ProcessId'];
  10. var options = {windowsHide: true, windowsVerbatimArguments: true};
  11. bin('wmic', args, options, function(err, stdout, code) {
  12. if (err) {
  13. callback(err);
  14. return;
  15. }
  16. if (code !== 0) {
  17. callback(new Error('pidtree wmic command exited with code ' + code));
  18. return;
  19. }
  20. // Example of stdout
  21. //
  22. // ParentProcessId ProcessId
  23. // 0 777
  24. try {
  25. stdout = stdout.split(os.EOL);
  26. var list = [];
  27. for (var i = 1; i < stdout.length; i++) {
  28. stdout[i] = stdout[i].trim();
  29. if (!stdout[i]) continue;
  30. stdout[i] = stdout[i].split(/\s+/);
  31. stdout[i][0] = parseInt(stdout[i][0], 10); // PPID
  32. stdout[i][1] = parseInt(stdout[i][1], 10); // PID
  33. list.push(stdout[i]);
  34. }
  35. callback(null, list);
  36. } catch (error) {
  37. callback(error);
  38. }
  39. });
  40. }
  41. module.exports = wmic;