0.3.0 • Published 7 years ago

wmic-extended v0.3.0

Weekly downloads
3
License
GPL-3.0
Repository
github
Last release
7 years ago

wmic-extended

WMIC command wrapper for Node.js

Introduction

This is an extended Node.js wrapper of Windows' WMIC command tool. It uses async.js to run commands and xml2js for results of GET and LIST options.

Important!

If you were using v0.1.0 after yo get v0.2.0, callback parameters have been changed. You must modify callbacks of Wmic.run() and Wmic.short() commands.

in v0.1.0 callbacks were like this:

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:

wmic.run(function(err, stdout, wmic) {})

// Or
Wmic.short({}, function(err, stdout, wmic) {})

Installation

Currently it is only available on npm. To install simply run the command npm install wmic-extended --save

After that you can simply require it in your js file:

const Wmic = require("wmic-extended")

Usage

This module is designed to cover WMIC command's all of options as much as possible.

Initializing

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

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
})

Building Command With Methods

There is several usefull methods to build your desired WMIC command before running:

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

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

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.

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.

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.

Building Command from Object

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"}
    ]
  }
})

Running Command and Getting Results

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.

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.

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
})

Shorthand to Run a Command

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.

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.
  }
)