2024-07-10 10:46:30 +00:00
|
|
|
// wmi.js
|
|
|
|
// WMI(Windows Management Instrumentation) API interface for WelsonJS framework
|
|
|
|
// Namhyeon Go (Catswords Research) <abuse@catswords.net>
|
|
|
|
// https://github.com/gnh1201/welsonjs
|
2020-11-14 20:35:53 +00:00
|
|
|
var WMIQueryObject = function() {
|
2020-11-14 19:21:51 +00:00
|
|
|
var wbemFlagReturnImmediately = 0x10;
|
|
|
|
var wbemFlagForwardOnly = 0x20;
|
|
|
|
|
2020-11-14 19:46:30 +00:00
|
|
|
this.computer = ".";
|
|
|
|
this.namespace = "root\\cimv2";
|
2024-07-10 10:46:30 +00:00
|
|
|
this._interface = null;
|
2020-11-14 19:21:51 +00:00
|
|
|
this.cursor = {};
|
|
|
|
this.current = {};
|
|
|
|
|
|
|
|
this.create = function() {
|
2020-11-14 19:38:23 +00:00
|
|
|
try {
|
|
|
|
if (typeof(GetObject) === "function") {
|
2024-07-10 10:46:30 +00:00
|
|
|
this._interface = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + this.computer + "\\" + this.namespace);
|
2020-11-14 19:38:23 +00:00
|
|
|
} else {
|
|
|
|
var objLocator = CreateObject("WbemScripting.SWbemLocator");
|
2024-07-10 10:46:30 +00:00
|
|
|
this._interface = objLocator.ConnectServer(this.computer, this.namespace);
|
2020-11-14 19:38:23 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
2020-11-14 19:30:18 +00:00
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:46:30 +00:00
|
|
|
this.setComputer = function(computer) {
|
|
|
|
this.computer = computer;
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:46:30 +00:00
|
|
|
this.setNamespace = function(namespace) {
|
|
|
|
this.namespace = namespace;
|
2020-11-14 19:21:51 +00:00
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:21:51 +00:00
|
|
|
this.execQuery = function(query) {
|
|
|
|
try {
|
2024-07-10 10:46:30 +00:00
|
|
|
var result = this._interface.ExecQuery(query, "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
|
2020-11-14 19:21:51 +00:00
|
|
|
this.cursor = new Enumerator(result);
|
2020-11-14 19:30:18 +00:00
|
|
|
} catch (e) {
|
2020-11-14 19:21:51 +00:00
|
|
|
console.error(e.message);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:21:51 +00:00
|
|
|
this.fetch = function() {
|
|
|
|
if (!this.cursor.atEnd()) {
|
|
|
|
this.current = this.cursor.item();
|
|
|
|
this.cursor.moveNext();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:21:51 +00:00
|
|
|
this.fetchAll = function() {
|
|
|
|
return this.cursor.toArray();
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-14 19:21:51 +00:00
|
|
|
this.get = function(key) {
|
|
|
|
if (key in this.current) {
|
|
|
|
return this.current[key];
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.create();
|
|
|
|
};
|
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
var WMIClassObject = function() {
|
2024-07-10 11:42:46 +00:00
|
|
|
this._interface = (new WMIQueryObject())._interface;
|
2020-11-15 04:31:35 +00:00
|
|
|
this.classObject = null;
|
|
|
|
this.className = "";
|
|
|
|
this.instance = null;
|
|
|
|
this.methodObject = null;
|
|
|
|
this.methodName = "";
|
|
|
|
this.inParams = {};
|
|
|
|
this.outParams = {};
|
|
|
|
|
|
|
|
// Instance
|
|
|
|
this.setClass = function(className) {
|
|
|
|
this.className = className;
|
2024-07-10 10:46:30 +00:00
|
|
|
this.classObject = this._interface.Get(className);
|
2020-11-15 04:31:35 +00:00
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.create = function() {
|
|
|
|
this.instance = this.classObject.SpawnInstance_();
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.getAttribute = function(key) {
|
|
|
|
return this.instance[key];
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.setAttribute = function(key, value) {
|
|
|
|
this.instance[key] = value;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.getInstance = function() {
|
|
|
|
return this.instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Method
|
|
|
|
this.setMethod = function(methodName) {
|
|
|
|
this.methodName = methodName;
|
|
|
|
this.methodObject = this.classObject.Methods_.Item(methodName);
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.setParameter = function(key, value) {
|
|
|
|
this.inParams[key] = value;
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.setParameters = function(params) {
|
|
|
|
if (typeof(params) !== "undefined") {
|
|
|
|
for (k in params) {
|
|
|
|
this.setParameter(k, params[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.execute = function() {
|
|
|
|
var params = this.methodObject.InParameters.SpawnInstance_();
|
|
|
|
for (k in this.parameters) {
|
|
|
|
params[k] = this.inParams[k];
|
|
|
|
}
|
|
|
|
this.outParams = this.classObject.ExecMethod_(this.methodName, params);
|
|
|
|
return this;
|
|
|
|
};
|
2022-04-11 02:15:30 +00:00
|
|
|
|
2020-11-15 04:31:35 +00:00
|
|
|
this.get = function(key) {
|
|
|
|
if (key in this.outParams) {
|
|
|
|
return this.outParams[key];
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-10-30 18:13:02 +00:00
|
|
|
function create() {
|
2024-07-10 11:42:46 +00:00
|
|
|
return new WMIQueryObject();
|
2023-10-30 18:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function execQuery(query) {
|
|
|
|
return create().execQuery(query);
|
2020-11-14 20:35:53 +00:00
|
|
|
};
|
|
|
|
|
2023-10-30 18:13:02 +00:00
|
|
|
function setClass(className) {
|
|
|
|
return create().setClass(className);
|
2020-11-14 19:21:51 +00:00
|
|
|
};
|
|
|
|
|
2023-10-30 18:13:02 +00:00
|
|
|
exports.create = create;
|
|
|
|
exports.execQuery = execQuery;
|
|
|
|
exports.setClass = setClass;
|
|
|
|
|
2024-07-10 11:42:46 +00:00
|
|
|
exports.VERSIONINFO = "WMI interface (wmi.js) version 0.1.3";
|
2020-11-14 19:21:51 +00:00
|
|
|
exports.global = global;
|
2022-04-11 02:15:30 +00:00
|
|
|
exports.require = global.require;
|