windows exec WMIC in try catch

This commit is contained in:
Sebastian Hildebrandt 2018-05-28 23:01:42 +02:00
parent fe9925ec99
commit 2ae03047ce
12 changed files with 791 additions and 681 deletions

View File

@ -100,6 +100,7 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.41.4 | 2018-05-28 | windows exec WMIC in try catch |
| 3.41.3 | 2018-05-13 | improved SunOS support `getStaticData()`, `getDynamicData()` |
| 3.41.2 | 2018-05-13 | bugfix `system()` and `flags()` Raspberry Pi |
| 3.41.1 | 2018-05-11 | updated docs |

View File

@ -141,6 +141,7 @@ module.exports = function (callback) {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' Path Win32_Battery Get BatteryStatus, DesignCapacity, EstimatedChargeRemaining /value', opts, function (error, stdout) {
if (stdout) {
let lines = stdout.split('\r\n');
@ -158,6 +159,10 @@ module.exports = function (callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -306,6 +306,7 @@ function getCpu() {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' cpu get name, description, revision, l2cachesize, l3cachesize, manufacturer, currentclockspeed, maxclockspeed /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n');
@ -370,6 +371,9 @@ function getCpu() {
resolve(result);
});
});
} catch (e) {
resolve(result);
}
}
});
});
@ -546,6 +550,7 @@ function cpuTemperature(callback) {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature', opts, function (error, stdout) {
if (!error) {
let sum = 0;
@ -563,6 +568,10 @@ function cpuTemperature(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
@ -579,6 +588,7 @@ function cpuFlags(callback) {
process.nextTick(() => {
let result = '';
if (_windows) {
try {
exec('reg query "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0" /v FeatureSet', opts, function (error, stdout) {
if (!error) {
let flag_hex = stdout.split('0x').pop().trim();
@ -602,6 +612,10 @@ function cpuFlags(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
if (_linux) {
exec('lscpu', function (error, stdout) {
@ -764,6 +778,7 @@ function cpuCache(callback) {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' cpu get l2cachesize, l3cachesize /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n');
@ -795,6 +810,10 @@ function cpuCache(callback) {
resolve(result);
});
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -78,6 +78,7 @@ function fsSize(callback) {
resolve(data);
}
if (_windows) {
try {
exec(util.getWmic() + ' logicaldisk get Caption,FileSystem,FreeSpace,Size', opts, function (error, stdout) {
let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
lines.forEach(function (line) {
@ -98,6 +99,10 @@ function fsSize(callback) {
}
resolve(data);
});
} catch (e) {
if (callback) { callback(data); }
resolve(data);
}
}
});
});
@ -167,7 +172,7 @@ function parseBlk(lines) {
lines.filter(line => line !== '').forEach((line) => {
line = util.decodeEscapeSequence(line);
line = line.replace(/\\/g,'\\\\');
line = line.replace(/\\/g, '\\\\');
let disk = JSON.parse(line);
data.push({
'name': disk.name,
@ -258,6 +263,7 @@ function blockDevices(callback) {
}
if (_windows) {
let drivetypes = ['Unknown', 'NoRoot', 'Removable', 'HDD', 'Network', 'CD/DVD', 'RAM'];
try {
exec(util.getWmic() + ' logicaldisk get Caption,Description,DeviceID,DriveType,FileSystem,FreeSpace,Name,Size,VolumeName,VolumeSerialNumber /value', opts, function (error, stdout) {
if (!error) {
let devices = stdout.toString().split(/\n\s*\n/);
@ -288,6 +294,11 @@ function blockDevices(callback) {
}
resolve(data);
});
} catch (e) {
if (callback) { callback(data); }
resolve(data);
}
}
});
});
@ -735,8 +746,8 @@ function diskLayout(callback) {
});
}
if (_windows) {
exec(util.getWmic() + ' diskdrive get /value', {encoding: 'utf8', windowsHide: true}, function (error, stdout) {
try {
exec(util.getWmic() + ' diskdrive get /value', { encoding: 'utf8', windowsHide: true }, function (error, stdout) {
if (!error) {
let devices = stdout.toString().split(/\n\s*\n/);
devices.forEach(function (device) {
@ -767,6 +778,10 @@ function diskLayout(callback) {
}
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -338,6 +338,7 @@ function graphics(callback) {
}
if (_windows) {
// https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/03/use-powershell-to-discover-multi-monitor-information/
try {
exec(util.getWmic() + ' path win32_VideoController get AdapterCompatibility, AdapterDACType, name, PNPDeviceID, CurrentVerticalResolution, CurrentHorizontalResolution, CurrentNumberOfColors, AdapterRAM, CurrentBitsPerPixel, CurrentRefreshRate, MinRefreshRate, MaxRefreshRate, VideoMemoryType /value', opts, function (error, stdout) {
if (!error) {
let csections = stdout.split(/\n\s*\n/);
@ -365,6 +366,10 @@ function graphics(callback) {
});
}
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -157,8 +157,9 @@ function inetLatency(host, callback) {
});
}
if (_windows) {
exec('ping ' + host + ' -n 1', opts, function (error, stdout) {
let result = -1;
try {
exec('ping ' + host + ' -n 1', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
lines.shift();
@ -174,6 +175,10 @@ function inetLatency(host, callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -199,6 +199,7 @@ function mem(callback) {
if (_windows) {
let swaptotal = 0;
let swapused = 0;
try {
exec(util.getWmic() + ' pagefile get AllocatedBaseSize, CurrentUsage', opts, function (error, stdout) {
if (!error) {
let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
@ -217,6 +218,10 @@ function mem(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
@ -228,7 +233,7 @@ function memLayout(callback) {
function getManufacturer(manId) {
if (OSX_RAM_manufacturers.hasOwnProperty(manId)) {
return(OSX_RAM_manufacturers[manId]);
return (OSX_RAM_manufacturers[manId]);
}
return manId;
}
@ -247,7 +252,7 @@ function memLayout(callback) {
let lines = device.split('\n');
if (parseInt(util.getValue(lines, 'Size'), 10) > 0) {
result.push({
size: parseInt(util.getValue(lines, 'Size'), 10)*1024*1024,
size: parseInt(util.getValue(lines, 'Size'), 10) * 1024 * 1024,
bank: util.getValue(lines, 'Bank Locator'),
type: util.getValue(lines, 'Type:'),
clockSpeed: (util.getValue(lines, 'Configured Clock Speed:') ? parseInt(util.getValue(lines, 'Configured Clock Speed:'), 10) : (util.getValue(lines, 'Speed:') ? parseInt(util.getValue(lines, 'Speed:'), 10) : -1)),
@ -332,6 +337,7 @@ function memLayout(callback) {
const memoryTypes = 'Unknown|Other|DRAM|Synchronous DRAM|Cache DRAM|EDO|EDRAM|VRAM|SRAM|RAM|ROM|FLASH|EEPROM|FEPROM|EPROM|CDRAM|3DRAM|SDRAM|SGRAM|RDRAM|DDR|DDR2|DDR2 FB-DIMM|Reserved|DDR3|FBD2|DDR4|LPDDR|LPDDR2|LPDDR3|LPDDR4'.split('|');
const FormFactors = 'Unknown|Other|SIP|DIP|ZIP|SOJ|Proprietary|SIMM|DIMM|TSOP|PGA|RIMM|SODIMM|SRIMM|SMD|SSMP|QFP|TQFP|SOIC|LCC|PLCC|BGA|FPBGA|LGA'.split('|');
try {
exec(util.getWmic() + ' memorychip get BankLabel, Capacity, ConfiguredClockSpeed, ConfiguredVoltage, MaxVoltage, MinVoltage, DataWidth, FormFactor, Manufacturer, MemoryType, PartNumber, SerialNumber, Speed, Tag /value', opts, function (error, stdout) {
if (!error) {
let devices = stdout.toString().split('BankL');
@ -356,6 +362,10 @@ function memLayout(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -366,6 +366,7 @@ function networkStats(iface, callback) {
let perfData = [];
let nics = [];
cmd = util.getWmic() + ' nic get MACAddress, name, NetEnabled /value';
try {
exec(cmd, opts, function (error, stdout) {
if (!error) {
const nsections = stdout.split(/\n\s*\n/);
@ -417,6 +418,10 @@ function networkStats(iface, callback) {
});
}
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
} else {
result.rx = _network[iface].rx;
@ -582,6 +587,7 @@ function networkConnections(callback) {
}
if (_windows) {
let cmd = 'netstat -na';
try {
exec(cmd, opts, function (error, stdout) {
if (!error) {
@ -635,6 +641,10 @@ function networkConnections(callback) {
resolve(result);
}
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -244,6 +244,7 @@ function osInfo(callback) {
if (_windows) {
result.logofile = getLogoFile();
result.release = result.kernel;
try {
exec(util.getWmic() + ' os get Caption', opts, function (error, stdout) {
result.distro = result.codename = stdout.slice(stdout.indexOf('\r\n') + 2).trim();
if (callback) {
@ -251,6 +252,10 @@ function osInfo(callback) {
}
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
@ -292,6 +297,7 @@ function versions(callback) {
};
})();
try {
exec('npm -v', function (error, stdout) {
if (!error) {
result.npm = stdout.toString().split('\n')[0];
@ -381,6 +387,10 @@ function versions(callback) {
}
functionProcessed();
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
});
});
}

View File

@ -140,6 +140,7 @@ function services(srv, callback) {
}
}
if (_windows) {
try {
exec(util.getWmic() + ' service get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let serviceSections = stdout.split(/\n\s*\n/);
@ -186,6 +187,10 @@ function services(srv, callback) {
resolve(data);
}
});
} catch (e) {
if (callback) { callback(data); }
resolve(data);
}
}
} else {
if (callback) { callback({}); }
@ -582,6 +587,7 @@ function processes(callback) {
});
}
if (_windows) {
try {
exec(util.getWmic() + ' process get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let processSections = stdout.split(/\n\s*\n/);
@ -666,6 +672,10 @@ function processes(callback) {
}
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
} else {
if (callback) { callback(_process_cpu.result); }

View File

@ -188,6 +188,7 @@ function system(callback) {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) {
if (!error) {
// let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/);
@ -209,6 +210,10 @@ function system(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
@ -261,6 +266,7 @@ function bios(callback) {
}
if (_windows) {
// TODO: check BIOS windows
try {
exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
@ -287,6 +293,10 @@ function bios(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
@ -350,6 +360,7 @@ function baseboard(callback) {
resolve(result);
}
if (_windows) {
try {
exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
@ -370,6 +381,10 @@ function baseboard(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});

View File

@ -245,6 +245,7 @@ function users(callback) {
});
}
if (_windows) {
try {
exec('query user', opts, function (error, stdout) {
if (stdout) {
// lines / split
@ -254,6 +255,10 @@ function users(callback) {
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});