EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
win-info.js
Go to the documentation of this file.
1/*
2Copyright 2019-2020 Intel Corporation
3
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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.
15*/
16
17var promise = require('promise');
18
19function qfe()
20{
21 try {
22 var tokens = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_QuickFixEngineering');
23 if (tokens[0]){
24 for (var index = 0; index < tokens.length; index++) {
25 for (var key in tokens[index]) {
26 if (key.startsWith('__')) delete tokens[index][key];
27 }
28 }
29 return (tokens);
30 } else {
31 return ([]);
32 }
33 } catch (ex) {
34 return ([]);
35 }
36}
37function av()
38{
39 var result = [];
40 try {
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;
49 var match;
50 while ((match = regex.exec(product.pathToSignedProductExe)) !== null) {
51 var envVar = match[1];
52 var envValue = process.env[envVar] || '';
53 if (envValue) {
54 modifiedPath = modifiedPath.replace(match[0], envValue);
55 }
56 }
57 // Check if the executable exists (unless it's Windows Defender pseudo-path)
58 var flag = true;
59 if (modifiedPath !== 'windowsdefender://') {
60 try {
61 if (!require('fs').existsSync(modifiedPath)) {
62 flag = false;
63 }
64 } catch (ex) {
65 flag = false;
66 }
67 }
68 // Only include products with valid executables
69 if (flag) {
70 var status = {};
71 status.product = product.displayName || '';
72 status.updated = (parseInt(product.productState) & 0x10) == 0;
73 status.enabled = (parseInt(product.productState) & 0x1000) == 0x1000;
74 result.push(status);
75 }
76 }
77 return (result);
78 } catch (ex) {
79 return ([]);
80 }
81}
82function defrag(options)
83{
84 var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
85 var path = '';
86
87 switch(require('os').arch())
88 {
89 case 'x64':
90 if (require('_GenericMarshal').PointerSize == 4)
91 {
92 // 32 Bit App on 64 Bit Windows
93 ret._rej('Cannot defrag volume on 64 bit Windows from 32 bit application');
94 return (ret);
95 }
96 else
97 {
98 // 64 Bit App
99 path = process.env['windir'] + '\\System32\\defrag.exe';
100 }
101 break;
102 case 'ia32':
103 // 32 Bit App on 32 Bit Windows
104 path = process.env['windir'] + '\\System32\\defrag.exe';
105 break;
106 default:
107 ret._rej(require('os').arch() + ' not supported');
108 return (ret);
109 break;
110 }
111
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)
118 {
119 var lines = this.stdout.str.trim().split('\r\n');
120 var obj = { volume: this.promise.options.volume };
121 for (var i in lines)
122 {
123 var token = lines[i].split('=');
124 if(token.length == 2)
125 {
126 switch(token[0].trim().toLowerCase())
127 {
128 case 'volume size':
129 obj['size'] = token[1];
130 break;
131 case 'free space':
132 obj['free'] = token[1];
133 break;
134 case 'total fragmented space':
135 obj['fragmented'] = token[1];
136 break;
137 case 'largest free space size':
138 obj['largestFragment'] = token[1];
139 break;
140 }
141 }
142 }
143 this.promise._res(obj);
144 });
145 return (ret);
146}
147function regQuery(H, Path, Key)
148{
149 try
150 {
151 return(require('win-registry').QueryKey(H, Path, Key));
152 }
153 catch(e)
154 {
155 return (null);
156 }
157}
158function pendingReboot()
159{
160 var tmp = null;
161 var ret = null;
162 var HKEY = require('win-registry').HKEY;
163 if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing', 'RebootPending') !=null)
164 {
165 ret = 'Component Based Servicing';
166 }
167 else if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate', 'RebootRequired'))
168 {
169 ret = 'Windows Update';
170 }
171 else if ((tmp=regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager', 'PendingFileRenameOperations'))!=null && tmp != 0 && tmp != '')
172 {
173 ret = 'File Rename';
174 }
175 else if (regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName', 'ComputerName') != regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName', 'ComputerName'))
176 {
177 ret = 'System Rename';
178 }
179 return (ret);
180}
181
182function installedApps()
183{
184 var promise = require('promise');
185 var ret = new promise(function (a, r) { this._resolve = a; this._reject = r; });
186
187 var code = "\
188 var reg = require('win-registry');\
189 var result = [];\
190 var val, tmp;\
191 var items = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall');\
192 for (var key in items.subkeys)\
193 {\
194 val = {};\
195 try\
196 {\
197 val.name = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayName');\
198 }\
199 catch(e)\
200 {\
201 continue;\
202 }\
203 try\
204 {\
205 val.version = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayVersion');\
206 if (val.version == '') { delete val.version; }\
207 }\
208 catch(e)\
209 {\
210 }\
211 try\
212 {\
213 val.location = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallLocation');\
214 if (val.location == '') { delete val.location; }\
215 }\
216 catch(e)\
217 {\
218 }\
219 try\
220 {\
221 val.installdate = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallDate');\
222 if (val.installdate == '') { delete val.installdate; }\
223 }\
224 catch(e)\
225 {\
226 }\
227 result.push(val);\
228 }\
229 console.log(JSON.stringify(result,'', 1));process.exit();";
230
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())); });
235 return (ret);
236}
237
238function installedStoreApps(){
239 try {
240 var tokens = require('win-wmi-fixed').query('ROOT\\CIMV2', 'SELECT * FROM Win32_InstalledStoreProgram');
241 if (tokens[0]){
242 for (var index = 0; index < tokens.length; index++) {
243 for (var key in tokens[index]) {
244 if (key.startsWith('__')) delete tokens[index][key];
245 }
246 }
247 return (tokens);
248 } else {
249 return ([]);
250 };
251 } catch (ex) {
252 return ([]);
253 }
254}
255
256function defender(){
257 try {
258 var tokens = require('win-wmi').query('ROOT\\Microsoft\\Windows\\Defender', 'SELECT * FROM MSFT_MpComputerStatus', ['RealTimeProtectionEnabled','IsTamperProtected','AntivirusSignatureVersion','AntivirusSignatureLastUpdated']);
259 if (tokens[0]){
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; }
263 return (info);
264 } else {
265 return ({});
266 }
267 } catch (ex) {
268 return ({});
269 }
270}
271
272if (process.platform == 'win32')
273{
274 module.exports = { qfe: qfe, av: av, defrag: defrag, pendingReboot: pendingReboot, installedApps: installedApps, installedStoreApps: installedStoreApps, defender: defender };
275}
276else
277{
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 };
280}