2020-11-14 19:21:51 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// WMI(Windows Management Instrumentation) API
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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";
|
2020-11-14 19:21:51 +00:00
|
|
|
this.interface = null;
|
|
|
|
this.cursor = {};
|
|
|
|
this.current = {};
|
|
|
|
|
|
|
|
this.create = function() {
|
2020-11-14 19:38:23 +00:00
|
|
|
try {
|
|
|
|
if (typeof(GetObject) === "function") {
|
2020-11-14 19: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");
|
2020-11-14 19: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 {
|
|
|
|
var result = this.interface.ExecQuery(query, "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
|
|
|
|
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() {
|
|
|
|
this.interface = (new WMIQueryObject()).interface;
|
|
|
|
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;
|
|
|
|
this.classObject = this.interface.Get(className);
|
|
|
|
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() {
|
|
|
|
return new WMIQueryObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-11-06 02:23:09 +00:00
|
|
|
exports.VERSIONINFO = "WMI interface (wmi.js) version 0.1.2";
|
2020-11-14 19:21:51 +00:00
|
|
|
exports.global = global;
|
2022-04-11 02:15:30 +00:00
|
|
|
exports.require = global.require;
|