-
Notifications
You must be signed in to change notification settings - Fork 7
Parse XML output of WMIC. #1
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,23 @@ | ||
| # wintools - Some windows tools for node.js # | ||
| # wintools - Some windows tools for node.js | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install wintools | ||
| ``` | ||
|
|
||
| * __ps(callback)__ where callback is `function(err, list)` returns a list of running processes. | ||
| * __kill.pid(pid, callback)__ kills a process by PID. | ||
| * __kill.image(imageName, callback)__ kills a process by image name (e.g. `node.exe`). | ||
| * __shutdown.poweroff([callback])__ turns of off the machine immediately. | ||
| * __shutdown.restart([callback])__ rstarts the machine immediately. | ||
| ```javascript | ||
| var wintools = require('wintools'); | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| * `wintools.ps(filter, callback)` where filter is a filter function `function(p)` executed on each process before pushing it to the list and callback is `function(err, list)` returns a list of running processes. | ||
| * `wintools.kill.pid(pid, callback)` kills a process by PID. | ||
| * `wintools.kill.image(imageName, callback)` kills a process by image name (e.g. `node.exe`). | ||
| * `wintools.shutdown.poweroff([callback])` turns of off the machine immediately. | ||
| * `wintools.shutdown.restart([callback])` rstarts the machine immediately. | ||
|
|
||
| ## License ## | ||
| ## License | ||
|
|
||
| MIT | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,48 @@ | ||
| var exec = require('child_process').exec; | ||
|
|
||
| var exec = require('child_process').exec; | ||
| /** | ||
| * Kills a process by PID. | ||
| * @param pid Required. Process ID. | ||
| * @param callback Optional. | ||
| * @remarks Windows only | ||
| */ | ||
| exports.pid = function(pid, callback) { | ||
| if (!pid) throw new Error('pid is required'); | ||
| if (!callback) callback = function() {}; | ||
| exec('taskkill /t /f /pid ' + pid.toString(), function (err, stdout, stderr) { | ||
| if (err) { | ||
| callback({ msg: "unable to kill " + pid, err: err, stdout: stdout, stderr: stderr }); | ||
| return; | ||
| } | ||
| callback(); | ||
| }); | ||
| }; | ||
|
|
||
| * Kills a process by PID. | ||
| * @param pid Required. Process ID. | ||
| * @param callback Optional. | ||
| * @remarks Windows only | ||
| */ | ||
| exports.pid = function (pid, callback) { | ||
| if (!pid) throw new Error('pid is required'); | ||
| if (!callback) callback = function () { }; | ||
| exec('taskkill /t /f /pid ' + pid.toString(), function (err, stdout, stderr) { | ||
| if (err) { | ||
| callback({ msg: "unable to kill " + pid, err: err, stdout: stdout, stderr: stderr }); | ||
| return; | ||
| } | ||
| callback(); | ||
| }); | ||
| }; | ||
| /** | ||
| * Kills all the processes with the specified image name | ||
| * @param imageName Required. The name of the image (e.g. 'node.exe') | ||
| * @param callback Optional. | ||
| */ | ||
| exports.image = function(imageName, callback) { | ||
| if (!imageName) throw new Error('imageName is required'); | ||
| if (!callback) callback = function() {}; | ||
|
|
||
| exec('taskkill /t /f /im ' + imageName, function (err, stdout, stderr) { | ||
| if (err) { | ||
| callback({ msg: "unable to kill " + imageName, err: err, stdout: stdout, stderr: stderr }); | ||
| return; | ||
| } | ||
|
|
||
| callback(); | ||
| }); | ||
| * Kills all the processes with the specified image name | ||
| * @param imageName Required. The name of the image (e.g. 'node.exe') | ||
| * @param callback Optional. | ||
| */ | ||
| exports.image = function (imageName, opts, callback) { | ||
| if (!imageName) throw new Error('imageName is required'); | ||
|
|
||
| if (typeof opts === 'function') { | ||
| callback = opts; | ||
| opts = ''; | ||
| } | ||
|
|
||
| if (!callback) callback = function () { }; | ||
|
|
||
| var cmd = ('taskkill /t /f /im ' + imageName + ' ' + opts).trim(); | ||
| console.log('cmd:', cmd); | ||
| exec(cmd, function (err, stdout, stderr) { | ||
| if (err) { | ||
| callback({ msg: "unable to kill " + imageName, err: err, stdout: stdout, stderr: stderr }); | ||
| return; | ||
| } | ||
|
|
||
| callback(); | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,60 @@ | ||
| var exec = require('child_process').exec; | ||
|
|
||
| var spawn = require('child_process').spawn; | ||
| var xml2js = require('xml2js'); | ||
|
|
||
| /** | ||
| * Returns all the system processes | ||
| * @param callback {function(err, list)} Called with the list of all processes. | ||
| * @remarks Runs only on Windows (uses WMI) | ||
| */ | ||
| module.exports = function(callback) { | ||
| if (!callback) callback = function(err, list) { }; | ||
|
|
||
| exec('wmic process list /format:csv', function (err, stdout, stderr) { | ||
| if (err) { | ||
| callback({ err: err, msg: "unable to enumerate processes" }); | ||
| return; | ||
| } | ||
|
|
||
| stdout = stdout.replace(/\r/g, '').split('\n').slice(1); | ||
| fields = stdout.shift().split(','); | ||
|
|
||
| var output = {}; | ||
| stdout.forEach(function (line) { | ||
|
|
||
| var parts = line.split(','); | ||
| var entry = {}; | ||
| for (var i = 0; i < fields.length; ++i) { | ||
| entry[fields[i]] = parts[i]; | ||
| } | ||
|
|
||
| var e = { | ||
| pid: entry.Handle, | ||
| desc: entry.Description, | ||
| cmd: entry.CommandLine, | ||
| prog: entry.ExecutablePath, | ||
| workingSet: entry.WorkingSetSize, | ||
| }; | ||
|
|
||
| // remove some empty stuff | ||
| if (!e.cmd) delete e.cmd; | ||
| if (!e.prog) delete e.prog; | ||
|
|
||
| if (e.pid) { | ||
| output[e.pid] = e; | ||
| } | ||
| }); | ||
|
|
||
| callback(null, output); | ||
| }); | ||
| * Returns all the system processes | ||
| * @param filter {function(p)->Boolean} A filter function executed on each process before pushing it to the list. | ||
| * @param callback {function(err, list)} Called with the list of all processes. | ||
| * @remarks Runs only on Windows (uses WMI) | ||
| */ | ||
| module.exports = function (filter, callback) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I suggest that instead of function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
options.filter = options.filter || function() { return true; };
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, absolutely. This would be much better. From: Elad Ben-Israel [reply@reply.github.com]
May I suggest that instead of function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
options.filter = options.filter || function() { return true; };Reply to this email directly or view it on GitHub:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks -----Original Message-----
Made this change Reply to this email directly or view it on GitHub: |
||
| if (!filter) filter = function () { return true; } | ||
| if (!callback) callback = function () { }; | ||
|
|
||
| var options = [ | ||
| 'process', | ||
| 'list', | ||
| '/format:rawxml' | ||
| ]; | ||
|
|
||
| // Spawn process, since output from exec can be too big for buffer size supported. | ||
| var p = spawn('wmic', options); | ||
|
|
||
| var xml = ''; | ||
|
|
||
| p.stdout.on('data', function (data) { | ||
| xml = xml + data.toString(); | ||
| }); | ||
|
|
||
| p.stderr.on('data', function (data) { | ||
| console.error(data); | ||
| }); | ||
|
|
||
| p.on('exit', function () { | ||
| parser = new xml2js.Parser(); | ||
| parser.parseString(xml, function (err, result) { | ||
| var output = {}; | ||
| result.RESULTS.CIM.INSTANCE.forEach(function (p) { | ||
| var entry = {}; | ||
| p.PROPERTY.forEach(function (v) { | ||
| entry[v['@'].NAME] = v.VALUE; | ||
| }); | ||
|
|
||
| var e = { | ||
| pid: entry.Handle, | ||
| desc: entry.Description, | ||
| cmd: entry.CommandLine, | ||
| prog: entry.ExecutablePath | ||
| }; | ||
|
|
||
| if (!e.cmd) delete e.cmd; | ||
| if (!e.prog) delete e.prog; | ||
|
|
||
| if (e.pid && filter(e)) { | ||
| output[e.pid] = e; | ||
| } | ||
| }); | ||
| callback(null, output); | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,35 @@ | ||
| { | ||
| "name": "wintools", | ||
| "description": "Some Windows tools for node.js", | ||
| "main": "./main", | ||
| "author": "Elad Ben-Israel", | ||
| "version": "0.1.0", | ||
| "dependencies": { | ||
| "async": ">=0.1.15" | ||
| }, | ||
| "devDependencies": { | ||
| "nodeunit": "*" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/anodejs/node-wintools" | ||
| }, | ||
| "scripts": { | ||
| "test": "node_modules/nodeunit/bin/nodeunit tests/" | ||
| } | ||
| "name": "wintools", | ||
| "description": "Some Windows tools for node.js", | ||
| "main": "./main", | ||
| "bin": {}, | ||
| "author": "anode <anode@microsoft.com>", | ||
| "version": "0.2.0", | ||
| "license": "MIT", | ||
| "contributors": [ | ||
| "Elad Ben-Israel <eladb@microsoft.com>", | ||
| "Yosef Dinerstein <yosefd@microsoft.com>" | ||
| ], | ||
| "keywords": [ | ||
| "nodejs", | ||
| "windows" | ||
| ], | ||
| "engines": { | ||
| "node": "~0.6.x" | ||
| }, | ||
| "dependencies": { | ||
| "async": "0.1.x", | ||
| "xml2js": "0.1.x" | ||
| }, | ||
| "devDependencies": { | ||
| "nodeunit": "0.6.x" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git://github.com/anodejs/node-wintools.git" | ||
| }, | ||
| "scripts": { | ||
| "test": "nodeunit test/*.js" | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,31 @@ | ||
| var wintools = require('../main'); | ||
|
|
||
| exports.ps = function(test) { | ||
| wintools.ps(function(err, ps) { | ||
| test.ok(!err, err); | ||
| test.ok(ps); | ||
| test.ok(ps && Object.keys(ps).length > 0); | ||
| test.done(); | ||
| }); | ||
| }; | ||
|
|
||
| exports.killByPID = function(test) { | ||
| wintools.kill.pid(77777, function(err) { | ||
| test.ok(err); | ||
| test.done(); | ||
| }); | ||
| }; | ||
|
|
||
| exports.killByImage = function(test) { | ||
| wintools.kill.image('bla.exe', function(err) { | ||
| test.ok(err); | ||
| test.done(); | ||
| }); | ||
| }; | ||
|
|
||
| exports.shutdown = function(test) { | ||
| // only check that api exists | ||
| test.ok(wintools.shutdown.poweroff); | ||
| test.ok(wintools.shutdown.restart); | ||
| test.done(); | ||
| var wintools = require('../main'); | ||
| exports.ps = function (test) { | ||
| wintools.ps(null, function (err, ps) { | ||
| test.ok(!err, err); | ||
| test.ok(ps); | ||
| test.ok(ps && Object.keys(ps).length > 0); | ||
| test.done(); | ||
| }); | ||
| }; | ||
| exports.killByPID = function (test) { | ||
| wintools.kill.pid(77777, function (err) { | ||
| test.ok(err); | ||
| test.done(); | ||
| }); | ||
| }; | ||
| exports.killByImage = function (test) { | ||
| wintools.kill.image('bla.exe', function (err) { | ||
| test.ok(err); | ||
| test.done(); | ||
| }); | ||
| }; | ||
| exports.shutdown = function (test) { | ||
| // only check that api exists | ||
| test.ok(wintools.shutdown.poweroff); | ||
| test.ok(wintools.shutdown.restart); | ||
| test.done(); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The double
#also works BTW...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jus copied this from your own version in role repository, that looked to me more updated :)
From: Elad Ben-Israel [reply@reply.github.com]
Sent: Wednesday, March 14, 2012 10:36 PM
To: Yosef Dinerstein
Subject: Re: [node-wintools] Parse XML output of WMIC. (#1)
The double
#also works BTW...Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r559384
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Its a hack...
Sent from my iPhone
On Mar 14, 2012, at 23:55, "Yosef Dinerstein" reply@reply.github.com wrote: