mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-06 00:13:33 +00:00
another wmic replacement
Signed-off-by: Simon Smith <simonsmith5521@gmail.com>
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -329,3 +329,5 @@ ASALocalRun/
|
||||
|
||||
# Klocwork
|
||||
.klocwork/
|
||||
|
||||
*.o
|
||||
@@ -1,3 +1,3 @@
|
||||
// This file is auto-generated, any edits may be overwritten
|
||||
#define SOURCE_COMMIT_DATE "2023-Apr-14 13:29:41-0700"
|
||||
#define SOURCE_COMMIT_HASH "ca52306f87407a122fac70723bfa025b9c422ae6"
|
||||
#define SOURCE_COMMIT_DATE "2024-Nov-3 15:40:18+0000"
|
||||
#define SOURCE_COMMIT_HASH "435bccd1eb335b2d54ad2057d8c40a477201df51"
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -560,32 +560,34 @@ function macos_identifiers()
|
||||
|
||||
function win_chassisType()
|
||||
{
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'SystemEnclosure', 'get', 'ChassisTypes']);
|
||||
// needs to be replaced with win-wmi but due to bug in win-wmi it doesnt handle arrays correctly
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], {});
|
||||
if (child == null) { return ([]); }
|
||||
child.descriptorMetadata = 'process-manager';
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('Get-CimInstance Win32_SystemEnclosure| Select-Object -ExpandProperty ChassisTypes\r\n');
|
||||
child.stdin.write('exit\r\n');
|
||||
child.waitExit();
|
||||
|
||||
try
|
||||
{
|
||||
var tok = child.stdout.str.split('{')[1].split('}')[0];
|
||||
var val = tok.split(',')[0];
|
||||
return (parseInt(val));
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
try {
|
||||
return (parseInt(child.stdout.str));
|
||||
} catch (e) {
|
||||
return (2); // unknown
|
||||
}
|
||||
}
|
||||
|
||||
function win_systemType()
|
||||
{
|
||||
var CSV = '/FORMAT:"' + require('util-language').wmicXslPath + 'csv"';
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'ComputerSystem', 'get', 'PCSystemType', CSV]);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
return (parseInt(child.stdout.str.trim().split(',').pop()));
|
||||
try {
|
||||
var tokens = require('win-wmi').query('ROOT\\CIMV2', 'SELECT PCSystemType FROM Win32_ComputerSystem', ['PCSystemType']);
|
||||
if (tokens[0]) {
|
||||
return (parseInt(tokens[0]['PCSystemType']));
|
||||
} else {
|
||||
return (parseInt(1)); // default is desktop
|
||||
}
|
||||
} catch (ex) {
|
||||
return (parseInt(1)); // default is desktop
|
||||
}
|
||||
}
|
||||
|
||||
function win_formFactor(chassistype)
|
||||
|
||||
@@ -1,343 +0,0 @@
|
||||
/*
|
||||
Copyright 2019-2021 Intel Corporation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// sysinfo is a helper module used to query various system metrics in a platform agnostic fashion
|
||||
//
|
||||
|
||||
|
||||
var PDH_FMT_LONG = 0x00000100;
|
||||
var PDH_FMT_DOUBLE = 0x00000200;
|
||||
|
||||
var promise = require('promise');
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
var GM = require('_GenericMarshal');
|
||||
GM.kernel32 = GM.CreateNativeProxy('kernel32.dll');
|
||||
GM.kernel32.CreateMethod('GlobalMemoryStatusEx'); // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex
|
||||
|
||||
GM.pdh = GM.CreateNativeProxy('pdh.dll');
|
||||
GM.pdh.CreateMethod('PdhAddEnglishCounterA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhaddenglishcountera
|
||||
GM.pdh.CreateMethod('PdhCloseQuery'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhclosequery
|
||||
GM.pdh.CreateMethod('PdhCollectQueryData'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhcollectquerydata
|
||||
GM.pdh.CreateMethod('PdhGetFormattedCounterValue'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcountervalue
|
||||
GM.pdh.CreateMethod('PdhGetFormattedCounterArrayA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcounterarraya
|
||||
GM.pdh.CreateMethod('PdhOpenQueryA'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhopenquerya
|
||||
GM.pdh.CreateMethod('PdhRemoveCounter'); // https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhremovecounter
|
||||
}
|
||||
|
||||
//
|
||||
// Windows Implementation for fetching CPU utilization
|
||||
//
|
||||
function windows_cpuUtilization()
|
||||
{
|
||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
p.counter = GM.CreateVariable(16);
|
||||
p.cpu = GM.CreatePointer();
|
||||
p.cpuTotal = GM.CreatePointer();
|
||||
var err = 0;
|
||||
if ((err = GM.pdh.PdhOpenQueryA(0, 0, p.cpu).Val) != 0) { p._rej(err); return; }
|
||||
|
||||
// This gets the CPU Utilization for each proc
|
||||
if ((err = GM.pdh.PdhAddEnglishCounterA(p.cpu.Deref(), GM.CreateVariable('\\Processor(*)\\% Processor Time'), 0, p.cpuTotal).Val) != 0) { p._rej(err); return; }
|
||||
|
||||
if ((err = GM.pdh.PdhCollectQueryData(p.cpu.Deref()).Val != 0)) { p._rej(err); return; }
|
||||
p._timeout = setTimeout(function (po)
|
||||
{
|
||||
// Query in 100ms
|
||||
|
||||
var u = { cpus: [] };
|
||||
var bufSize = GM.CreateVariable(4);
|
||||
var itemCount = GM.CreateVariable(4);
|
||||
var buffer, szName, item;
|
||||
var e;
|
||||
if ((e = GM.pdh.PdhCollectQueryData(po.cpu.Deref()).Val != 0)) { po._rej(e); return; /* Reject Promise if this fails */ }
|
||||
|
||||
if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, 0).Val) == -2147481646)
|
||||
{
|
||||
buffer = GM.CreateVariable(bufSize.toBuffer().readUInt32LE());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Reject Promise if this failed
|
||||
po._rej(e);
|
||||
return;
|
||||
}
|
||||
if ((e = GM.pdh.PdhGetFormattedCounterArrayA(po.cpuTotal.Deref(), PDH_FMT_DOUBLE, bufSize, itemCount, buffer).Val) != 0) { po._rej(e); return; /* reject promise on failuer */ }
|
||||
for(var i=0;i<itemCount.toBuffer().readUInt32LE();++i) // Enumerate the counter array
|
||||
{
|
||||
item = buffer.Deref(i * 24, 24);
|
||||
szName = item.Deref(0, GM.PointerSize).Deref();
|
||||
if (szName.String == '_Total')
|
||||
{
|
||||
u.total = item.Deref(16, 8).toBuffer().readDoubleLE(); // Add Value for TOTAL
|
||||
}
|
||||
else
|
||||
{
|
||||
u.cpus[parseInt(szName.String)] = item.Deref(16, 8).toBuffer().readDoubleLE(); // Add Value for CPU index
|
||||
}
|
||||
}
|
||||
|
||||
GM.pdh.PdhRemoveCounter(po.cpuTotal.Deref());
|
||||
GM.pdh.PdhCloseQuery(po.cpu.Deref());
|
||||
p._res(u);
|
||||
}, 100, p);
|
||||
|
||||
return (p);
|
||||
}
|
||||
|
||||
//
|
||||
// Windows Implementation to fetch memory utilization
|
||||
//
|
||||
function windows_memUtilization()
|
||||
{
|
||||
var info = GM.CreateVariable(64);
|
||||
info.Deref(0, 4).toBuffer().writeUInt32LE(64);
|
||||
GM.kernel32.GlobalMemoryStatusEx(info); // Fetch the memory Utilization Metrics
|
||||
|
||||
var ret =
|
||||
{
|
||||
MemTotal: require('bignum').fromBuffer(info.Deref(8, 8).toBuffer(), { endian: 'little' }), // Convert total to bignum type
|
||||
MemFree: require('bignum').fromBuffer(info.Deref(16, 8).toBuffer(), { endian: 'little' }) // Convert free to bignum type
|
||||
};
|
||||
|
||||
// Calculate the remaining metrics, and convert to bignum type
|
||||
ret.percentFree = ((ret.MemFree.div(require('bignum')('1048576')).toNumber() / ret.MemTotal.div(require('bignum')('1048576')).toNumber()) * 100);//.toFixed(2);
|
||||
ret.percentConsumed = ((ret.MemTotal.sub(ret.MemFree).div(require('bignum')('1048576')).toNumber() / ret.MemTotal.div(require('bignum')('1048576')).toNumber()) * 100);//.toFixed(2);
|
||||
ret.MemTotal = ret.MemTotal.toString();
|
||||
ret.MemFree = ret.MemFree.toString();
|
||||
return (ret);
|
||||
}
|
||||
|
||||
var cpuLastIdle = [];
|
||||
var cpuLastSum = [];
|
||||
|
||||
|
||||
//
|
||||
// Linux Implementation of fetching the CPU Utilization, which uses procfs
|
||||
//
|
||||
function linux_cpuUtilization() {
|
||||
var ret = { cpus: [] };
|
||||
var info = require('fs').readFileSync('/proc/stat'); // fetch metrics from procfs
|
||||
var lines = info.toString().split('\n');
|
||||
var columns;
|
||||
var x, y;
|
||||
var cpuNo = 0;
|
||||
var currSum, currIdle, utilization;
|
||||
for (var i in lines) {
|
||||
columns = lines[i].split(' ');
|
||||
if (!columns[0].startsWith('cpu')) { break; }
|
||||
|
||||
// Parse out the utilization metrics, calulcating the values if necessary
|
||||
x = 0, currSum = 0;
|
||||
while (columns[++x] == '');
|
||||
for (y = x; y < columns.length; ++y) { currSum += parseInt(columns[y]); }
|
||||
currIdle = parseInt(columns[3 + x]);
|
||||
|
||||
var diffIdle = currIdle - cpuLastIdle[cpuNo];
|
||||
var diffSum = currSum - cpuLastSum[cpuNo];
|
||||
|
||||
utilization = (100 - ((diffIdle / diffSum) * 100));
|
||||
|
||||
cpuLastSum[cpuNo] = currSum;
|
||||
cpuLastIdle[cpuNo] = currIdle;
|
||||
|
||||
if (!ret.total) {
|
||||
ret.total = utilization;
|
||||
} else {
|
||||
ret.cpus.push(utilization);
|
||||
}
|
||||
++cpuNo;
|
||||
}
|
||||
|
||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
p._res(ret);
|
||||
return (p);
|
||||
}
|
||||
|
||||
//
|
||||
// Linux implementation of memory utilization, using procfs
|
||||
//
|
||||
function linux_memUtilization()
|
||||
{
|
||||
var ret = {};
|
||||
|
||||
var info = require('fs').readFileSync('/proc/meminfo').toString().split('\n');
|
||||
var tokens;
|
||||
for(var i in info)
|
||||
{
|
||||
tokens = info[i].split(' ');
|
||||
switch(tokens[0])
|
||||
{
|
||||
case 'MemTotal:':
|
||||
ret.total = parseInt(tokens[tokens.length - 2]);
|
||||
break;
|
||||
case 'MemFree:':
|
||||
ret.free = parseInt(tokens[tokens.length - 2]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret.percentFree = ((ret.free / ret.total) * 100);//.toFixed(2);
|
||||
ret.percentConsumed = (((ret.total - ret.free) / ret.total) * 100);//.toFixed(2);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//
|
||||
// macOS CPU Utilization, where we just use system utility 'top'
|
||||
//
|
||||
function macos_cpuUtilization()
|
||||
{
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write('top -l 1 | grep -E "^CPU"\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.split('\n');
|
||||
if (lines[0].length > 0)
|
||||
{
|
||||
var usage = lines[0].split(':')[1];
|
||||
var bdown = usage.split(',');
|
||||
|
||||
var tot = parseFloat(bdown[0].split('%')[0].trim()) + parseFloat(bdown[1].split('%')[0].trim());
|
||||
ret._res({total: tot, cpus: []});
|
||||
}
|
||||
else
|
||||
{
|
||||
ret._rej('parse error');
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//
|
||||
// macOS Memory Utilization, using system utility 'top'
|
||||
//
|
||||
function macos_memUtilization()
|
||||
{
|
||||
var mem = { };
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = '';
|
||||
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
|
||||
child.stdin.write('top -l 1 | grep -E "^Phys"\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.split('\n');
|
||||
if (lines[0].length > 0)
|
||||
{
|
||||
var usage = lines[0].split(':')[1];
|
||||
var bdown = usage.split(',');
|
||||
|
||||
mem.MemTotal = parseInt(bdown[0].trim().split(' ')[0]);
|
||||
mem.MemFree = parseInt(bdown[1].trim().split(' ')[0]);
|
||||
mem.percentFree = ((mem.MemFree / mem.MemTotal) * 100);//.toFixed(2);
|
||||
mem.percentConsumed = (((mem.MemTotal - mem.MemFree) / mem.MemTotal) * 100);//.toFixed(2);
|
||||
return (mem);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw ('Parse Error');
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Windows Implementation of fetching system thermals, using WMI and system utility 'wmic'
|
||||
//
|
||||
function windows_thermals()
|
||||
{
|
||||
var ret = [];
|
||||
child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', '/namespace:\\\\root\\wmi', 'PATH', 'MSAcpi_ThermalZoneTemperature', 'get', 'CurrentTemperature']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
if(child.stdout.str.trim!='')
|
||||
{
|
||||
var lines = child.stdout.str.trim().split('\r\n');
|
||||
for (var i = 1; i < lines.length; ++i)
|
||||
{
|
||||
if (lines[i].trim() != '') { ret.push(((parseFloat(lines[i]) / 10) - 273.15).toFixed(2)); }
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//
|
||||
// Linux Implementation of fetching system thermals, from the filesystem
|
||||
//
|
||||
function linux_thermals()
|
||||
{
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write("cat /sys/class/thermal/thermal_zone*/temp | awk '{ print $0 / 1000 }'\nexit\n");
|
||||
child.waitExit();
|
||||
var ret = child.stdout.str.trim().split('\n');
|
||||
if (ret.length == 1 && ret[0] == '') { ret = []; }
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//
|
||||
// macOS Implementation of fetching system thermals, using system utility 'powermetrics'
|
||||
//
|
||||
function macos_thermals()
|
||||
{
|
||||
var ret = [];
|
||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.on('data', function () { });
|
||||
child.stdin.write('powermetrics --help | grep SMC\nexit\n');
|
||||
child.waitExit();
|
||||
|
||||
if (child.stdout.str.trim() != '')
|
||||
{
|
||||
child = require('child_process').execFile('/bin/sh', ['sh']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c)
|
||||
{
|
||||
this.str += c.toString();
|
||||
var tokens = this.str.trim().split('\n');
|
||||
for (var i in tokens)
|
||||
{
|
||||
if (tokens[i].split(' die temperature: ').length > 1)
|
||||
{
|
||||
ret.push(tokens[i].split(' ')[3]);
|
||||
this.parent.kill();
|
||||
}
|
||||
}
|
||||
});
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stdin.write('powermetrics -s smc\n');
|
||||
child.waitExit(5000);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
switch(process.platform)
|
||||
{
|
||||
case 'linux':
|
||||
module.exports = { cpuUtilization: linux_cpuUtilization, memUtilization: linux_memUtilization, thermals: linux_thermals };
|
||||
break;
|
||||
case 'win32':
|
||||
module.exports = { cpuUtilization: windows_cpuUtilization, memUtilization: windows_memUtilization, thermals: windows_thermals };
|
||||
break;
|
||||
case 'darwin':
|
||||
module.exports = { cpuUtilization: macos_cpuUtilization, memUtilization: macos_memUtilization, thermals: macos_thermals };
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,260 +0,0 @@
|
||||
/*
|
||||
Copyright 2019-2020 Intel Corporation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// This module fetches various Windows System information, such as pending reboot status,
|
||||
// volume defrag state, installed applications, windows update status, etc
|
||||
//
|
||||
|
||||
|
||||
var promise = require('promise');
|
||||
|
||||
//
|
||||
// This function queries WMI to fetch Windows Update Status
|
||||
//
|
||||
function qfe()
|
||||
{
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'qfe', 'list', 'full', '/FORMAT:CSV']);
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
child.waitExit();
|
||||
|
||||
var lines = child.stdout.str.trim().split('\r\n');
|
||||
var keys = lines[0].split(',');
|
||||
var i, key;
|
||||
var tokens;
|
||||
var result = [];
|
||||
|
||||
for (i = 1; i < lines.length; ++i)
|
||||
{
|
||||
var obj = {};
|
||||
tokens = lines[i].split(',');
|
||||
for (key = 0; key < keys.length; ++key)
|
||||
{
|
||||
if (tokens[key]) { obj[keys[key]] = tokens[key]; }
|
||||
}
|
||||
result.push(obj);
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
// This function uses Windows Powershell to fetch metadata about the currently configured AntiVirus
|
||||
function av()
|
||||
{
|
||||
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], {});
|
||||
if (child == null) { return ([]); }
|
||||
|
||||
child.descriptorMetadata = 'process-manager';
|
||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
|
||||
child.stdin.write('[reflection.Assembly]::LoadWithPartialName("system.core")\r\n');
|
||||
child.stdin.write('Get-WmiObject -Namespace "root/SecurityCenter2" -Class AntiVirusProduct | ');
|
||||
child.stdin.write('ForEach-Object -Process { ');
|
||||
child.stdin.write('$Bytes = [System.Text.Encoding]::UTF8.GetBytes($_.displayName); ');
|
||||
child.stdin.write('$EncodedText =[Convert]::ToBase64String($Bytes); ');
|
||||
child.stdin.write('Write-Host ("{0},{1}" -f $_.productState,$EncodedText); }\r\n');
|
||||
child.stdin.write('exit\r\n');
|
||||
child.waitExit();
|
||||
|
||||
if (child.stdout.str == '') { return ([]); }
|
||||
|
||||
var lines = child.stdout.str.trim().split('\r\n');
|
||||
var result = [];
|
||||
for (i = 0; i < lines.length; ++i)
|
||||
{
|
||||
var keys = lines[i].split(',');
|
||||
if(keys.length == 2)
|
||||
{
|
||||
var status = {};
|
||||
status.product = Buffer.from(keys[1], 'base64').toString();
|
||||
status.updated = (parseInt(keys[0]) & 0x10) == 0;
|
||||
status.enabled = (parseInt(keys[0]) & 0x1000) == 0x1000;
|
||||
result.push(status);
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
//
|
||||
// This function uses the defrag system utility to query defrag state of the specified volume
|
||||
//
|
||||
// Note: options.volume must be specified
|
||||
function defrag(options)
|
||||
{
|
||||
var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||
var path = '';
|
||||
|
||||
switch(require('os').arch())
|
||||
{
|
||||
case 'x64':
|
||||
if (require('_GenericMarshal').PointerSize == 4)
|
||||
{
|
||||
// 32 Bit App on 64 Bit Windows
|
||||
ret._rej('Cannot defrag volume on 64 bit Windows from 32 bit application');
|
||||
return (ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 64 Bit App
|
||||
path = process.env['windir'] + '\\System32\\defrag.exe';
|
||||
}
|
||||
break;
|
||||
case 'ia32':
|
||||
// 32 Bit App on 32 Bit Windows
|
||||
path = process.env['windir'] + '\\System32\\defrag.exe';
|
||||
break;
|
||||
default:
|
||||
ret._rej(require('os').arch() + ' not supported');
|
||||
return (ret);
|
||||
break;
|
||||
}
|
||||
|
||||
ret.child = require('child_process').execFile(process.env['windir'] + '\\System32\\defrag.exe', ['defrag', options.volume + ' /A']);
|
||||
ret.child.promise = ret;
|
||||
ret.child.promise.options = options;
|
||||
ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
ret.child.stderr.str = ''; ret.child.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||
ret.child.on('exit', function (code)
|
||||
{
|
||||
var lines = this.stdout.str.trim().split('\r\n');
|
||||
var obj = { volume: this.promise.options.volume };
|
||||
for (var i in lines)
|
||||
{
|
||||
var token = lines[i].split('=');
|
||||
if(token.length == 2)
|
||||
{
|
||||
switch(token[0].trim().toLowerCase())
|
||||
{
|
||||
case 'volume size':
|
||||
obj['size'] = token[1];
|
||||
break;
|
||||
case 'free space':
|
||||
obj['free'] = token[1];
|
||||
break;
|
||||
case 'total fragmented space':
|
||||
obj['fragmented'] = token[1];
|
||||
break;
|
||||
case 'largest free space size':
|
||||
obj['largestFragment'] = token[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.promise._res(obj);
|
||||
});
|
||||
return (ret);
|
||||
}
|
||||
|
||||
// Helper/Shortcut for registry query
|
||||
function regQuery(H, Path, Key)
|
||||
{
|
||||
try
|
||||
{
|
||||
return(require('win-registry').QueryKey(H, Path, Key));
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
}
|
||||
|
||||
// This function returns details on any system pending reboots
|
||||
function pendingReboot()
|
||||
{
|
||||
var tmp = null;
|
||||
var ret = null;
|
||||
var HKEY = require('win-registry').HKEY;
|
||||
if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing', 'RebootPending') !=null)
|
||||
{
|
||||
ret = 'Component Based Servicing';
|
||||
}
|
||||
else if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate', 'RebootRequired'))
|
||||
{
|
||||
ret = 'Windows Update';
|
||||
}
|
||||
else if ((tmp=regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager', 'PendingFileRenameOperations'))!=null && tmp != 0 && tmp != '')
|
||||
{
|
||||
ret = 'File Rename';
|
||||
}
|
||||
else if (regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName', 'ComputerName') != regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName', 'ComputerName'))
|
||||
{
|
||||
ret = 'System Rename';
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//
|
||||
// Returns a promise that fetches the list of installed applications
|
||||
//
|
||||
function installedApps()
|
||||
{
|
||||
var promise = require('promise');
|
||||
var ret = new promise(function (a, r) { this._resolve = a; this._reject = r; });
|
||||
|
||||
var code = "\
|
||||
var reg = require('win-registry');\
|
||||
var result = [];\
|
||||
var val, tmp;\
|
||||
var items = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall');\
|
||||
for (var key in items.subkeys)\
|
||||
{\
|
||||
val = {};\
|
||||
try\
|
||||
{\
|
||||
val.name = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayName');\
|
||||
}\
|
||||
catch(e)\
|
||||
{\
|
||||
continue;\
|
||||
}\
|
||||
try\
|
||||
{\
|
||||
val.version = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayVersion');\
|
||||
if (val.version == '') { delete val.version; }\
|
||||
}\
|
||||
catch(e)\
|
||||
{\
|
||||
}\
|
||||
try\
|
||||
{\
|
||||
val.location = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallLocation');\
|
||||
if (val.location == '') { delete val.location; }\
|
||||
}\
|
||||
catch(e)\
|
||||
{\
|
||||
}\
|
||||
result.push(val);\
|
||||
}\
|
||||
console.log(JSON.stringify(result,'', 1));process.exit();";
|
||||
|
||||
ret.child = require('child_process').execFile(process.execPath, [process.execPath.split('\\').pop().split('.exe')[0], '-exec "' + code + '"']);
|
||||
ret.child.promise = ret;
|
||||
ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||
ret.child.on('exit', function (c) { this.promise._resolve(JSON.parse(this.stdout.str.trim())); });
|
||||
return (ret);
|
||||
}
|
||||
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
module.exports = { qfe: qfe, av: av, defrag: defrag, pendingReboot: pendingReboot, installedApps: installedApps };
|
||||
}
|
||||
else
|
||||
{
|
||||
var not_supported = function () { throw (process.platform + ' not supported'); };
|
||||
module.exports = { qfe: not_supported, av: not_supported, defrag: not_supported, pendingReboot: not_supported, installedApps: not_supported };
|
||||
}
|
||||
Reference in New Issue
Block a user