2Copyright 2019-2020 Intel Corporation
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
17var promise = require('promise');
22 var tokens = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_QuickFixEngineering');
24 for (var index = 0; index < tokens.length; index++) {
25 for (var key in tokens[index]) {
26 if (key.startsWith('__')) delete tokens[index][key];
41 var tokens = require('win-wmi-fixed').query('ROOT\\SecurityCenter2', 'SELECT * FROM AntiVirusProduct');
42 if (tokens.length == 0) { return ([]); }
43 // Process each antivirus product
44 for (var i = 0; i < tokens.length; ++i) {
45 var product = tokens[i];
46 var modifiedPath = product.pathToSignedProductExe || '';
47 // Expand environment variables (e.g., %ProgramFiles%)
48 var regex = /%([^%]+)%/g;
50 while ((match = regex.exec(product.pathToSignedProductExe)) !== null) {
51 var envVar = match[1];
52 var envValue = process.env[envVar] || '';
54 modifiedPath = modifiedPath.replace(match[0], envValue);
57 // Check if the executable exists (unless it's Windows Defender pseudo-path)
59 if (modifiedPath !== 'windowsdefender://') {
61 if (!require('fs').existsSync(modifiedPath)) {
68 // Only include products with valid executables
71 status.product = product.displayName || '';
72 status.updated = (parseInt(product.productState) & 0x10) == 0;
73 status.enabled = (parseInt(product.productState) & 0x1000) == 0x1000;
82function defrag(options)
84 var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
87 switch(require('os').arch())
90 if (require('_GenericMarshal').PointerSize == 4)
92 // 32 Bit App on 64 Bit Windows
93 ret._rej('Cannot defrag volume on 64 bit Windows from 32 bit application');
99 path = process.env['windir'] + '\\System32\\defrag.exe';
103 // 32 Bit App on 32 Bit Windows
104 path = process.env['windir'] + '\\System32\\defrag.exe';
107 ret._rej(require('os').arch() + ' not supported');
112 ret.child = require('child_process').execFile(process.env['windir'] + '\\System32\\defrag.exe', ['defrag', options.volume + ' /A']);
113 ret.child.promise = ret;
114 ret.child.promise.options = options;
115 ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
116 ret.child.stderr.str = ''; ret.child.stderr.on('data', function (c) { this.str += c.toString(); });
117 ret.child.on('exit', function (code)
119 var lines = this.stdout.str.trim().split('\r\n');
120 var obj = { volume: this.promise.options.volume };
123 var token = lines[i].split('=');
124 if(token.length == 2)
126 switch(token[0].trim().toLowerCase())
129 obj['size'] = token[1];
132 obj['free'] = token[1];
134 case 'total fragmented space':
135 obj['fragmented'] = token[1];
137 case 'largest free space size':
138 obj['largestFragment'] = token[1];
143 this.promise._res(obj);
147function regQuery(H, Path, Key)
151 return(require('win-registry').QueryKey(H, Path, Key));
158function pendingReboot()
162 var HKEY = require('win-registry').HKEY;
163 if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing', 'RebootPending') !=null)
165 ret = 'Component Based Servicing';
167 else if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate', 'RebootRequired'))
169 ret = 'Windows Update';
171 else if ((tmp=regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager', 'PendingFileRenameOperations'))!=null && tmp != 0 && tmp != '')
175 else if (regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName', 'ComputerName') != regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName', 'ComputerName'))
177 ret = 'System Rename';
182function installedApps()
184 var promise = require('promise');
185 var ret = new promise(function (a, r) { this._resolve = a; this._reject = r; });
188 var reg = require('win-registry');\
191 var items = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall');\
192 for (var key in items.subkeys)\
197 val.name = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayName');\
205 val.version = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayVersion');\
206 if (val.version == '') { delete val.version; }\
213 val.location = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallLocation');\
214 if (val.location == '') { delete val.location; }\
221 val.installdate = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallDate');\
222 if (val.installdate == '') { delete val.installdate; }\
229 console.log(JSON.stringify(result,'', 1));process.exit();";
231 ret.child = require('child_process').execFile(process.execPath, [process.execPath.split('\\').pop().split('.exe')[0], '-exec "' + code + '"']);
232 ret.child.promise = ret;
233 ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
234 ret.child.on('exit', function (c) { this.promise._resolve(JSON.parse(this.stdout.str.trim())); });
238function installedStoreApps(){
240 var tokens = require('win-wmi-fixed').query('ROOT\\CIMV2', 'SELECT * FROM Win32_InstalledStoreProgram');
242 for (var index = 0; index < tokens.length; index++) {
243 for (var key in tokens[index]) {
244 if (key.startsWith('__')) delete tokens[index][key];
258 var tokens = require('win-wmi').query('ROOT\\Microsoft\\Windows\\Defender', 'SELECT * FROM MSFT_MpComputerStatus', ['RealTimeProtectionEnabled','IsTamperProtected','AntivirusSignatureVersion','AntivirusSignatureLastUpdated']);
260 var info = { RealTimeProtection: tokens[0].RealTimeProtectionEnabled, TamperProtected: tokens[0].IsTamperProtected };
261 if (tokens[0].AntivirusSignatureVersion) { info.AntivirusSignatureVersion = tokens[0].AntivirusSignatureVersion; }
262 if (tokens[0].AntivirusSignatureLastUpdated) { info.AntivirusSignatureLastUpdated = tokens[0].AntivirusSignatureLastUpdated; }
272if (process.platform == 'win32')
274 module.exports = { qfe: qfe, av: av, defrag: defrag, pendingReboot: pendingReboot, installedApps: installedApps, installedStoreApps: installedStoreApps, defender: defender };
278 var not_supported = function () { throw (process.platform + ' not supported'); };
279 module.exports = { qfe: not_supported, av: not_supported, defrag: not_supported, pendingReboot: not_supported, installedApps: not_supported, installedStoreApps: not_supported, defender: not_supported };