processes() fix command line parsing (win) wip

This commit is contained in:
Sebastian Hildebrandt 2025-12-27 17:37:18 +01:00
parent f6acf2e30e
commit 22d2b355d5

View File

@ -1128,69 +1128,70 @@ function processLoad(proc, callback) {
if (procSanitized && processes.length && processes[0] !== '------') {
if (_windows) {
try {
util.powerShell('Get-CimInstance Win32_Process | select ProcessId,Caption,UserModeTime,KernelModeTime,WorkingSetSize | fl').then((stdout, error) => {
util.powerShell('Get-CimInstance Win32_Process | select ProcessId,Caption,UserModeTime,KernelModeTime,WorkingSetSize | ConvertTo-Json -compress').then((stdout, error) => {
if (!error) {
let processSections = stdout.split(/\n\s*\n/);
let procStats = [];
let list_new = {};
const procStats = [];
const list_new = {};
let allcpuu = 0;
let allcpus = 0;
let processArray = [];
try {
stdout = stdout.trim().replace(/^\uFEFF/, '');
processArray = JSON.parse(stdout);
} catch {}
// go through all processes
processSections.forEach((element) => {
if (element.trim() !== '') {
let lines = element.trim().split('\r\n');
let pid = parseInt(util.getValue(lines, 'ProcessId', ':', true), 10);
let name = util.getValue(lines, 'Caption', ':', true);
let utime = parseInt(util.getValue(lines, 'UserModeTime', ':', true), 10);
let stime = parseInt(util.getValue(lines, 'KernelModeTime', ':', true), 10);
let mem = parseInt(util.getValue(lines, 'WorkingSetSize', ':', true), 10);
allcpuu = allcpuu + utime;
allcpus = allcpus + stime;
processArray.forEach((element) => {
const pid = element.ProcessId;
const name = element.Caption;
const utime = element.UserModeTime;
const stime = element.KernelModeTime;
const mem = element.WorkingSetSize;
allcpuu = allcpuu + utime;
allcpus = allcpus + stime;
procStats.push({
pid: pid,
name,
utime: utime,
stime: stime,
cpu: 0,
cpuu: 0,
cpus: 0,
mem
});
let pname = '';
let inList = false;
processes.forEach((proc) => {
if (name.toLowerCase().indexOf(proc.toLowerCase()) >= 0 && !inList) {
inList = true;
pname = proc;
procStats.push({
pid: pid,
name,
utime: utime,
stime: stime,
cpu: 0,
cpuu: 0,
cpus: 0,
mem
});
let pname = '';
let inList = false;
processes.forEach((proc) => {
if (name.toLowerCase().indexOf(proc.toLowerCase()) >= 0 && !inList) {
inList = true;
pname = proc;
}
});
if (processesString === '*' || inList) {
let processFound = false;
result.forEach((item) => {
if (item.proc.toLowerCase() === pname.toLowerCase()) {
item.pids.push(pid);
item.mem += (mem / os.totalmem()) * 100;
processFound = true;
}
});
if (processesString === '*' || inList) {
let processFound = false;
result.forEach((item) => {
if (item.proc.toLowerCase() === pname.toLowerCase()) {
item.pids.push(pid);
item.mem += (mem / os.totalmem()) * 100;
processFound = true;
}
if (!processFound) {
result.push({
proc: pname,
pid: pid,
pids: [pid],
cpu: 0,
mem: (mem / os.totalmem()) * 100
});
if (!processFound) {
result.push({
proc: pname,
pid: pid,
pids: [pid],
cpu: 0,
mem: (mem / os.totalmem()) * 100
});
}
}
}
});
// add missing processes
if (processesString !== '*') {
// add missing processes
let processesMissing = processes.filter((name) => procStats.filter((item) => item.name.toLowerCase().indexOf(name) >= 0).length === 0);
processesMissing.forEach((procName) => {
result.push({
@ -1239,7 +1240,7 @@ function processLoad(proc, callback) {
resolve(result);
}
});
} catch (e) {
} catch {
if (callback) {
callback(result);
}