An extended helper for Windows wmic command tool
npm install wmic-extendedGET and LIST options.Wmic.run() and Wmic.short() commands.in v0.1.0 callbacks were like this:
``javascript
wmic.run(function(err, stdout, stderr, wmic) {})
// Or
Wmic.short({}, function(err, stdout, stderr, wmic) {})
`
in now v0.2.0 console output errors passed as first argument:
`javascript
wmic.run(function(err, stdout, wmic) {})
// Or
Wmic.short({}, function(err, stdout, wmic) {})
`
After that you can simply require it in your js file:
`javascript
const Wmic = require("wmic-extended")
`Usage
This module is designed to cover WMIC command's all of options as much as possible.
$3
Wmic is a class of methods that needs to be initialized. Also you can run a command without
initializing the class but we will get to there.Example usage
`javascript
const Wmic = require("wmic-extended")var wmic = new Wmic()
wmic.alias("computersystem").Args.list("full")
wmic.run(function(err, stdout, wmic) {
console.dir(stdout) // xml2js formatted Object
})
`$3
There is several usefull methods to build your desired WMIC command before running:`javascript
const Wmic = require("wmic-extended")var wmic = new Wmic()
wmic.alias("computersystem").Args.list("full")
// Equivalent to: WMIC COMPUTERSYSTEM LIST FULL /FORMAT:RAWXML
`The builded command areas are:
WMIC [global switches] [alias] [where clauses] [option] [parameters] [switches]> All Wmic class methods that do not return any value are chainable
#### Global Switches
`javascript
var wmic = new Wmic()
wmic.node(string|array)
wmic.failfast(boolean)
wmic.user(string)
wmic.password(string)
wmic.privileges(boolean)
wmic.namespace(string)
wmic.role(string)
wmic.trace(string)
wmic.implevel(string)
wmic.authlevel(string)
wmic.aggregate(boolean)
`These are currently available global switches. Each method adds up to an array. If you want to modify added switches
you can just call
wmic.globalSwitches variable at any time.
#### Alias
`javascript
var wmic = new Wmic()
wmic.alias(string)
`This method determines the alias. You can run
wmic /? from command prompt to see list of available aliases.
#### Where clauses
There is a seperated
where class that initializes when wmic initialized. You can get this class simply by calling wmic.Where
variable.`javascript
var wmic = new Wmic()
where = wmic.Where
where.clause(property, operator|value, value) // alias of and
where.and(property, operator|value, value)
where.or(property, operator|value, value)
`The first where clause does not matter if it is
clause(), and(), or() method; It will remove the glue (and|or) from the first where clause. - First parameter is the property name.
- Second parameter is operator. But you can define a value. Default behavior is
= operator. wmic.and("name", "somevalue") is equivalent to wmic.and("name", "=", "somewalue")
- Third parameter is the value. If the value was given in the second parameter, this parameter will not operate.
#### Arguments
To define options, parameters and switches you need to call
Args object from Wmic first.`javascript
var wmic = new Wmic()
args = wmic.Args
args.list(parameters) // String
args.get(parameters) // String|Array
args.call(method, parameters) // method: string, parameters: array
args.set(parameters) // Array
args.create(parameters) // Array
args.delete()
args.assoc()args.switch(name, value) // name: string, value: string
`call(), set() and create() methods should be array of containing objects with the format of {var: "", val: ""}Also some of these options uses switches. You can define them with
Args.switch() method. Each call will add a switch
to the array. If you want to modify these array, you can call Args._switches variable.
$3
`javascript
var wmic = new Wmic()
wmic.fromObject({
// GlobalSwitches
node: ["computer1", "laptop2"],
user: "user",
password: "pass",
alias: "computersystem",
where: [
[null, "property", "=", "value"]
],
args: {
option: "list",
parameters: "full",
switches: [
{name: "format", value="rawxml"}
]
}
})
`$3
After you built your command you need to call
run(callback) command and provide a callback to it. Its uses async.js' queue() method to run commands asynchronously.`javascript
wmic.alias("computersystem").Args.list("full")
wmic.run(function(error, stdout, wmic) {
if (error) return console.error(error)
console.dir(stdout) // Object output converted by xml2js
})
`If
format switch defined as rawxml or did not defined at all, it will format list and get options' the
output using xml2js. If you want to disable this behavior, you need to set wmic.autoProcessOutput to false.`javascript
wmic.alias("computersystem").Args.list("full")
wmic.autoProcessOutput = false
wmic.run(function(error, stdout, wmic) {
if (error) return console.error(error)
console.log(stdout) // raw text output
})
`$3
Wmic class provides a static method to run wmic commands with ease: Wmic.short(options, callback). The options
parameter is exactly same with fromObject method.`javascript
Wmic.short(
{
// GlobalSwitches
node: ["computer1", "laptop2"],
user: "user",
password: "pass",
alias: "computersystem",
where: [
[null, "property", "=", "value"]
],
args: {
option: "list",
parameters: "full",
switches: [
{name: "format", value="rawxml"}
]
}
},
function(error, stdout, wmic) {
if (error) return console.error(error)
console.log(stdout) // parsed xml data.
}
)``