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