Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass PID list into wmic query to speed things up on Windows. #73

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var SystemEOL = require('os').EOL;
* @param {Object[]} callback.stdout
*/

var Exec = module.exports = exports = function (args, callback) {
var Exec = module.exports = exports = function (winArgs, args, callback) {
var spawn = ChildProcess.spawn;

// on windows, if use ChildProcess.exec(`wmic process get`), the stdout will gives you nothing
Expand Down Expand Up @@ -64,7 +64,7 @@ var Exec = module.exports = exports = function (args, callback) {
callback(stderr, stdout.join(SystemEOL) || false);
});

CMD.stdin.write('wmic process get ProcessId,ParentProcessId,CommandLine \n');
CMD.stdin.write('wmic process ' + winArgs.join(" ") + ' get ProcessId,ParentProcessId,CommandLine \n');
CMD.stdin.end();
}
else {
Expand Down Expand Up @@ -119,6 +119,7 @@ exports.lookup = function (query, callback) {
* add 'lx' as default ps arguments, since the default ps output in linux like "ubuntu", wont include command arguments
*/
var exeArgs = query.psargs || ['lx'];
var winArgs = [];
var filter = {};
var idList;

Expand All @@ -131,7 +132,7 @@ exports.lookup = function (query, callback) {
else {
idList = [query.pid];
}

winArgs = ["where", '"' + idList.map(pid => "ProcessId="+pid).join(" OR ") + '"'];
// Cast all PIDs as Strings
idList = idList.map(function (v) {
return String(v);
Expand All @@ -152,7 +153,7 @@ exports.lookup = function (query, callback) {
filter['ppid'] = new RegExp(query.ppid);
}

return Exec(exeArgs, function (err, output) {
return Exec(winArgs, exeArgs, function (err, output) {
if (err) {
return callback(err);
}
Expand Down