welsonjs/lib/system.js

173 lines
5.0 KiB
JavaScript
Raw Normal View History

2020-07-07 17:06:38 +00:00
////////////////////////////////////////////////////////////////////////
// System API
////////////////////////////////////////////////////////////////////////
2020-07-18 22:52:32 +00:00
var SHELL = require("lib/shell");
2020-07-07 17:06:38 +00:00
var WSH = CreateObject("WScript.Shell");
2020-11-14 19:21:51 +00:00
var WMI = require("lib/wmi");
2020-07-07 17:06:38 +00:00
exports.VERSIONINFO = "System Module (system.js) version 0.1";
exports.global = global;
exports.require = global.require;
2020-07-21 06:30:18 +00:00
exports.createProcess = function(cmd) {
var SW_HIDE = 0;
//wmi.Get("Win32_Process").Create(cmd, null, si, pid);
2020-11-14 20:49:28 +00:00
return WMI.setClass("Win32_Process").setMethod("Create").setParameters({
2020-11-14 20:35:53 +00:00
"CommandLine": cmd,
"CurrentDirectory": null,
2020-11-14 20:49:28 +00:00
"ProcessStartupInformation": WMI.setClass("Win32_ProcessStartup").create()
.setAttribute("ShowWindow", SW_HIDE)
.setAttribute("CreateFlags", 16777216)
.setAttribute("X", 1)
.setAttribute("Y", 1)
.setAttribute("xSize", 1)
.setAttribute("ySize", 1)
.getInstance()
}).execute().get("ProcessID");
2020-07-21 06:30:18 +00:00
};
2020-11-04 07:30:52 +00:00
exports.getEnvString = function(envName) {
return WSH.ExpandEnvironmentStrings('%' + envName + '%');
2020-07-21 06:30:18 +00:00
};
exports.get32BitFolder = function() {
2020-07-29 03:06:49 +00:00
var base = exports.getEnvString("WINDIR");
2020-07-21 06:30:18 +00:00
var syswow64 = base + "\\SysWOW64\\";
if (JPTUDBSTOW.FS.FolderExists(syswow64))
return syswow64;
return base + "\\System32\\";
}
exports.isElevated = function() {
2020-07-07 17:06:38 +00:00
try {
WSH.RegRead("HKEY_USERS\\s-1-5-19\\");
return true;
} catch (e) {
return false;
}
};
2020-11-04 07:30:52 +00:00
exports.getOS = function() {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("SELECT * FROM Win32_OperatingSystem").fetch().get("Caption").rtrim();
2020-07-07 17:06:38 +00:00
};
exports.getDCName = function() {
2020-07-07 17:06:38 +00:00
try {
var DC = WSH.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\History\\DCName");
if (DC.length > 0)
return DC;
} catch (e) {}
};
exports.getArch = function() {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("SELECT * FROM Win32_OperatingSystem").fetch().get("OSArchitecture");
2020-07-07 17:06:38 +00:00
};
exports.getUUID = function() {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("SELECT * FROM Win32_ComputerSystemProduct").fetch().get("UUID").toLowerCase();
2020-07-07 17:06:38 +00:00
};
exports.getCurrentWorkingDirectory = function() {
2020-07-07 17:06:38 +00:00
try {
cwd = SHELL.exec("cd", "cwd.txt").rtrim();
return cwd;
} catch (e) {}
};
2020-08-05 07:35:43 +00:00
exports.getDirName = function(path) {
var delimiter = "\\";
var pos = path.lastIndexOf(delimiter);
return (pos > -1 ? path.substring(0, pos) : "");
};
exports.getFileName = function(path) {
var delimiter = "\\";
var pos = path.lastIndexOf(delimiter);
return (pos > -1 ? path.substring(pos + delimiter.length) : "");
};
exports.getCurrentScriptDirectory = function() {
2020-08-05 07:35:43 +00:00
if (typeof(WScript) !== "undefined") {
return exports.getDirName(WScript.ScriptFullName);
} else if (typeof(document) !== "undefined") {
return exports.getDirName(document.location.pathname);
2020-07-21 09:01:38 +00:00
} else {
return ".";
}
2020-07-07 17:06:38 +00:00
};
2020-07-23 02:05:57 +00:00
exports.getCurrentScriptName = function() {
2020-11-04 07:30:52 +00:00
if (typeof(WScript) !== "undefined") {
2020-07-23 02:05:57 +00:00
return WScript.ScriptName;
2020-08-05 07:35:43 +00:00
} else if (typeof(document) !== "undefined") {
return exports.getFileName(document.location.pathname);
2020-07-23 02:05:57 +00:00
} else {
return "";
}
};
exports.getNetworkInterfaces = function() {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("SELECT * FROM Win32_NetworkAdapterConfiguration").fetchAll();
2020-07-07 17:06:38 +00:00
};
2020-11-15 04:31:35 +00:00
exports.getProcessList = function() {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("Select * From Win32_Process").fetchAll();
2020-11-13 08:44:58 +00:00
};
2020-11-27 06:58:43 +00:00
exports.getPIDList = function() {
var result = [];
var processes = exports.getProcessList();
for (var i = 0; i < processes.length; i++) {
result.push(processes[i].ProcessID);
}
return result;
};
2020-11-27 13:31:51 +00:00
exports.isAlivePID = function(pid) {
if (!pid) {
return false;
} else {
return (exports.getPIDList().indexOf(pid) > -1);
}
};
2020-11-15 04:31:35 +00:00
exports.getProcessListByName = function(name) {
2020-11-27 13:31:51 +00:00
return exports.getProcessList().filter(function(s) {
return (s.Caption === name);
});
2020-11-15 04:31:35 +00:00
};
2020-07-21 06:30:18 +00:00
exports.killProcess = function(pid) {
2020-11-27 06:58:43 +00:00
var processes = exports.getProcessList();
2020-07-21 06:30:18 +00:00
2020-11-13 09:47:03 +00:00
for (var i = 0; i < processes.length; i++) {
2020-07-21 06:30:18 +00:00
try {
2020-11-13 09:47:03 +00:00
if (processes[i].ProcessId == pid) {
processes[i].Terminate();
2020-07-21 06:30:18 +00:00
return true;
}
2020-11-27 06:58:43 +00:00
} catch (e) {
console.error("Lib -> System -> killProcess() -> ", e.message);
}
2020-07-07 17:16:41 +00:00
}
2020-07-21 06:30:18 +00:00
return false;
2020-07-07 17:16:41 +00:00
};
exports.createShortcut = function(shoutcutName, fileName) {
var workingDirectory = exports.getCurrentWorkingDirectory();
2020-07-18 22:52:32 +00:00
var desktopPath = WSH.SpecialFolders("Desktop");
var link = WSH.CreateShortcut(desktopPath + "\\" + shoutcutName + ".lnk");
link.IconLocation = fileName + ",1";
link.TargetPath = workingDirectory + "\\" + fileName;
2020-11-20 08:44:50 +00:00
link.WindowStyle = 1;
2020-07-18 22:52:32 +00:00
link.WorkingDirectory = workingDirectory;
link.Save();
};
2020-11-05 06:38:51 +00:00
2020-11-10 09:13:41 +00:00
exports.ping = function(address) {
2020-11-14 19:21:51 +00:00
return WMI.execQuery("Select * From Win32_PingStatus where address='" + address + "'").fetch().get("ResponseTime");
2020-11-05 06:38:51 +00:00
};