Skip to content

[fix] resolves #58 on newer win32 systems #59

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

Open
wants to merge 1 commit into
base: master
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
35 changes: 34 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,38 @@ module.exports = function childrenOfPid(pid, callback) {
// System 0 4
// smss.exe 4 228
// ```
//
// Win32 without wmic:
// TODO
// 1. wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)
// 2. The order of head columns is fixed
// ```powershell
// > Get-WmiObject -Class Win32_Process | Select-Object -Property Name,ProcessId,ParentProcessId,Status | Format-Table
//(this is an empty line returned by powershell script)
// Name ProcessId ParentProcessId Status
// ---- --------- --------------- ------
// System Idle Process 0 0
// System 4 0
// smss.exe 228
// ```

var processLister;
if (process.platform === 'win32') {
// See also: https://github.com/nodejs/node-v0.x-archive/issues/2318
// Trying with WMIC
processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,Status']);
processLister.on('error', (code) => {
// Drop error indicating that creating WMIC failed
});

// check if WMIC exists
if (!processLister.connected) {
// Trying with powershell
processLister = spawn('powershell.exe',['Get-WmiObject -Class Win32_Process | Select-Object -Property Name,ProcessId,ParentProcessId,Status | Format-Table']);
processLister.on('error', (code) => {
// Drop error indicating that creating Powershell failed too; results in empty list
});
}
} else {
processLister = spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm']);
}
Expand All @@ -57,8 +84,14 @@ module.exports = function childrenOfPid(pid, callback) {
// spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm']).stdout,
processLister.stdout,
es.split(),
es.map(function (line, cb) { //this could parse alot of unix command output
es.map(function (line, cb) { //this could parse a lot of unix command output
var columns = line.trim().split(/\s+/);

// Drop additional lines from output of win32 powershell
if ((line.length === 0) || (columns[0] === ('----'))) {
return cb();
}

if (!headers) {
headers = columns;

Expand Down