-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpowershell.js
More file actions
70 lines (60 loc) 路 2.08 KB
/
Copy pathpowershell.js
File metadata and controls
70 lines (60 loc) 路 2.08 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
var os = require('os');
var bin = require('./bin');
// PowerShell command used to list every process as "<ppid> <pid>" lines.
// Get-CimInstance is the supported replacement for the removed wmic utility
// (see https://github.com/simonepri/pidtree/issues/20).
// $ProgressPreference is silenced so PowerShell does not serialize its
// progress stream (e.g. "Preparing modules for first use.") to stderr as
// CLIXML, which would otherwise be treated as an error.
var COMMAND =
"$ProgressPreference = 'SilentlyContinue'; " +
'Get-CimInstance -ClassName Win32_Process | ' +
'ForEach-Object { "$($_.ParentProcessId) $($_.ProcessId)" }';
// The command is passed through -EncodedCommand (Base64 of the UTF-16LE
// string) to avoid any cross-shell argument quoting issues on Windows.
var ENCODED = Buffer.from(COMMAND, 'utf16le').toString('base64');
/**
* Gets the list of all the pids of the system through PowerShell.
* Used as a fallback on Windows installations where wmic is not available
* (e.g. Windows 11 24H2 and Windows Server 2025).
* @param {Function} callback(err, list)
*/
function powershell(callback) {
var args = ['-NoProfile', '-NonInteractive', '-EncodedCommand', ENCODED];
var options = {windowsHide: true};
bin('powershell', args, options, function(err, stdout, code) {
if (err) {
callback(err);
return;
}
if (code !== 0) {
callback(
new Error('pidtree powershell command exited with code ' + code)
);
return;
}
// Example of stdout
//
// 0 777
// 777 778
// 0 779
try {
stdout = stdout.split(os.EOL);
var list = [];
for (var i = 0; i < stdout.length; i++) {
stdout[i] = stdout[i].trim();
if (!stdout[i]) continue;
var parts = stdout[i].split(/\s+/);
var ppid = parseInt(parts[0], 10); // PPID
var pid = parseInt(parts[1], 10); // PID
if (isNaN(ppid) || isNaN(pid)) continue;
list.push([ppid, pid]);
}
callback(null, list);
} catch (error) {
callback(error);
}
});
}
module.exports = powershell;