mirror of
https://github.com/fofolee/uTools-ProcessKiller.git
synced 2025-06-09 06:54:33 +08:00
使用 Get-Process 替代 tasklist
This commit is contained in:
parent
ccae084082
commit
2b66c4959d
69
preload.js
69
preload.js
@ -1,26 +1,59 @@
|
|||||||
const exec = require('child_process').exec;
|
const fs = require('fs');
|
||||||
const iconv = require('iconv-lite');
|
const path = require("path");
|
||||||
|
const iconExtractor = require('icon-extractor');
|
||||||
|
const os = require('os')
|
||||||
|
const PowerShell = require("powershell");
|
||||||
|
|
||||||
window.tasklist = (callback) => {
|
|
||||||
// if(process.platform === 'win32'){}
|
getico = tasks =>{
|
||||||
let cmd = 'tasklist /FO csv /NH /V'
|
iconExtractor.emitter.on('icon', function (data) {
|
||||||
exec(cmd, function (err, stdout, stderr) {
|
let icondir = path.join(os.tmpdir(), 'ProcessIcon')
|
||||||
|
fs.exists(icondir, exists => {
|
||||||
|
if (!exists) { fs.mkdirSync(icondir) }
|
||||||
|
let iconpath = path.join(icondir, `${data.Context}.png`)
|
||||||
|
fs.exists(iconpath, exists => {
|
||||||
|
if (!exists) {
|
||||||
|
fs.writeFile(iconpath, data.Base64ImageData, "base64", err => {
|
||||||
|
if (err) { console.log(err); }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var task of tasks) {
|
||||||
|
iconExtractor.getIcon(task.ProcessName, task.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasklist = (callback) => {
|
||||||
|
let ps = new PowerShell("chcp 65001;Get-Process | Format-List ProcessName,Path,Description");
|
||||||
|
ps.on("output", data => {
|
||||||
let tasklist = [];
|
let tasklist = [];
|
||||||
let lines = stdout.trim().split('\n');
|
let tasks = data.trim().split('\r\n\r\n');
|
||||||
|
for (var task of tasks) {
|
||||||
|
dict = {}
|
||||||
|
let lines = task.split('\r\n')
|
||||||
for (var line of lines) {
|
for (var line of lines) {
|
||||||
tasklist.push(line.trim().split(','));
|
if (line) {
|
||||||
|
let key = line.split(/\s+:\s*/)[0];
|
||||||
|
let value = line.split(/\s+:\s*/)[1];
|
||||||
|
dict[key] = value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
var icon = path.join(os.tmpdir(), 'ProcessIcon', `${dict.ProcessName}.png`);
|
||||||
|
dict.Icon = icon
|
||||||
|
tasklist.push(dict);
|
||||||
|
}
|
||||||
|
tasklist.shift();
|
||||||
|
getico(tasklist);
|
||||||
callback(tasklist);
|
callback(tasklist);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
window.taskkill = (taskname, callback) => {
|
taskkill = (taskname, callback) => {
|
||||||
let cmd = 'TASKKILL /F /IM "' + taskname + '" /T'
|
let ps = new PowerShell(`chcp 65001;Stop-Process -Name ${taskname}`);
|
||||||
exec(cmd, { encoding: "buffer"}, (err, stdout, stderr) => {
|
ps.on("error-output", data => {
|
||||||
if (err) {
|
callback(data.split('\n')[0])
|
||||||
callback(iconv.decode(stderr,'cp936'));
|
});
|
||||||
return;
|
|
||||||
}
|
|
||||||
callback(iconv.decode(stdout,'cp936'));
|
|
||||||
})
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user