-
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
Open
yosefd
wants to merge
4
commits into
master
Choose a base branch
from
yosefd1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(options, callback)` where 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,52 @@ | ||
| 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 (!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(); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,69 @@ | ||
| 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 options | ||
| * filter {function(p)} filters processes before inserting to result list. | ||
| * @param callback {function(err, list)} Called with the list of all processes. | ||
| * @remarks Runs only on Windows (uses WMI) | ||
| */ | ||
| module.exports = function (options, callback) { | ||
|
|
||
| if (typeof options === 'function') { | ||
| callback = options; | ||
| options = {}; | ||
| } | ||
|
|
||
| options = options || {}; | ||
| options.filter = options.filter || function () { return true; }; | ||
|
|
||
| callback = callback || function () { }; | ||
|
|
||
| var cmd = [ | ||
| 'process', | ||
| 'list', | ||
| '/format:rawxml' | ||
| ]; | ||
|
|
||
| // Spawn process, since output from exec can be too big for buffer size supported. | ||
| var p = spawn('wmic', cmd); | ||
|
|
||
| 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 && options.filter(e)) { | ||
| output[e.pid] = e; | ||
| } | ||
| }); | ||
| callback(null, output); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.1", | ||
| "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" | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(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(); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: