welsonjs/lib/system.js

187 lines
5.1 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");
var WMI = GetObject("winmgmts:\\\\.\\root\\CIMV2");
2020-07-07 19:38:34 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
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;
var pid = 0;
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")
var si = wmi.Get("Win32_ProcessStartup").SpawnInstance_();
si.ShowWindow = SW_HIDE;
si.CreateFlags = 16777216;
si.X = si.Y = si.XSize = si.ySize = 1;
//wmi.Get("Win32_Process").Create(cmd, null, si, pid);
var w32proc = wmi.Get("Win32_Process");
var method = w32proc.Methods_.Item("Create");
var inParams = method.InParameters.SpawnInstance_();
inParams.CommandLine = cmd;
inParams.CurrentDirectory = null;
inParams.ProcessStartupInformation = si;
var outParams = w32proc.ExecMethod_("Create", inParams);
return outParams.ProcessId;
};
2020-07-21 06:32:23 +00:00
exports.getEnvString = function(path) {
2020-07-21 06:30:18 +00:00
return WSH.ExpandEnvironmentStrings(path);
};
exports.get32BitFolder = function() {
2020-07-21 06:32:23 +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;
}
};
exports.getOS = function() {
2020-07-07 17:06:38 +00:00
try {
var colItems = WMI.ExecQuery("SELECT * FROM Win32_OperatingSystem");
var enumItems = new Enumerator(colItems);
var objItem = enumItems.item();
return objItem.Caption.rtrim();
} catch (e) {}
};
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-07-07 17:06:38 +00:00
try {
var colItems = WMI.ExecQuery("SELECT * FROM Win32_OperatingSystem");
var enumItems = new Enumerator(colItems);
var objItem = enumItems.item();
return objItem.OSArchitecture;
} catch (e) {}
};
exports.getUUID = function() {
2020-07-07 17:06:38 +00:00
try {
var colItems = WMI.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct");
var enumItems = new Enumerator(colItems);
var objItem = enumItems.item();
return objItem.UUID.toLowerCase();
} catch (e) {}
};
exports.getCurrentWorkingDirectory = function() {
2020-07-07 17:06:38 +00:00
try {
cwd = SHELL.exec("cd", "cwd.txt").rtrim();
return cwd;
} catch (e) {}
};
// "console only";
exports.getCurrentScriptDirectory = function() {
2020-07-21 09:01:38 +00:00
if(typeof(WScript) !== "undefined") {
var path = WScript.ScriptFullName;
var pos = path.lastIndexOf("\\");
return path.substring(0, pos);
} else {
return ".";
}
2020-07-07 17:06:38 +00:00
};
2020-07-23 02:05:57 +00:00
// "console only";
exports.getCurrentScriptName = function() {
if(typeof(WScript) !== "undefined") {
return WScript.ScriptName;
} else {
return "";
}
};
exports.getNetworkInterfaces = function() {
2020-07-07 17:06:38 +00:00
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var rows = [];
try {
var colItems = WMI.ExecQuery(
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True",
"WQL",
wbemFlagReturnImmediately | wbemFlagForwardOnly
);
var enumItems = new Enumerator(colItems);
for (; !enumItems.atEnd(); enumItems.moveNext()) {
var objItem = enumItems.item();
try {
rows.push({
Caption: objItem.Caption,
IPAddresses: objItem.IPAddress.toArray(),
MACAddress: objItem.MACAddress
});
} catch(e) {}
}
} catch(e) {}
return rows;
};
2020-07-21 06:30:18 +00:00
exports.getProcessList = function() {
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");
var query = "Select * From Win32_Process";
return wmi.ExecQuery(query);
};
exports.killProcess = function(pid) {
var processes = exports.getProcessList();
var items = new Enumerator(processes);
while (!items.atEnd()) {
var proc = items.item();
try {
if (proc.ProcessId == pid) {
proc.Terminate();
return true;
}
} catch (e) {}
2020-07-07 17:16:41 +00:00
2020-07-21 06:30:18 +00:00
items.moveNext();
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;
link.WindowStyle = 3;
link.WorkingDirectory = workingDirectory;
link.Save();
};