-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathwsh_sample.js
101 lines (89 loc) · 3.45 KB
/
wsh_sample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var win32ole = require('win32ole');
win32ole.print('wsh_sample\n');
console.log(win32ole.version());
var path = require('path');
var cwd = path.join(win32ole.MODULEDIRNAME, '..');
var fs = require('fs');
var tmpdir = path.join(cwd, 'test/tmp');
if(!fs.existsSync(tmpdir)) fs.mkdirSync(tmpdir);
var outfile = path.join(tmpdir, 'wsh_sample.txt');
var wsh_sample = function(filename){
var sh = win32ole.client.Dispatch('WScript.Shell');
console.log('sh:');
// console.log(require('util').inspect(sh.__, true, null, true)); // {}
//console.log(require('util').inspect(sh, true, null, true)); // *** error
try{
if(fs.existsSync(filename)) fs.unlinkSync(filename);
win32ole.print('notepad ...');
// arg1=1: movetop (default), 2: minimize, 3: maximize, 4: no movetop
// 5: movetop, 6: minimize, 7: minimize
// arg2=false: Async (default), true: Sync
sh.Run('notepad.exe', 1, false); // must be Async (for SendKeys)
win32ole.sleep(3000, true, false);
sh.SendKeys('Congratulations!{ENTER}'); // 'Run' option must be 1
win32ole.sleep(200);
sh.SendKeys("*** DON'T TOUCH THIS WINDOW ***{ENTER}");
win32ole.sleep(3000);
sh.SendKeys('Saving filename will be entered automatically.');
win32ole.sleep(3000);
sh.SendKeys('%f'); // ALT-F (File)
win32ole.sleep(500);
sh.SendKeys('a'); // SaveAs
win32ole.sleep(5000);
sh.SendKeys(filename + '{ENTER}');
win32ole.sleep(1000);
sh.SendKeys('%{F4}'); // ALT-F4 (Exit)
win32ole.sleep(100);
win32ole.print('ok\n');
win32ole.print('ping ...');
sh.Run('ping.exe 127.0.0.1', 4, true); // wait for close (Sync)
win32ole.print('ok\n');
}catch(e){
console.log('(exception catched)' + e);
}
var shellexec = function(sh, cmd, callback){
console.log('sh.Exec(' + cmd + ')');
var stat = sh.Exec(cmd);
// while(stat.Status == 0) win32ole.sleep(100, true, true);
var so = stat.StdOut._; // ***
/*
In ParseUnaryExpression() < v8/src/parser.cc >
v8::Object::ToBoolean() is called directly for unary operator '!'
instead of v8::Object::valueOf()
so NamedPropertyHandler will not be called
Local<Boolean> ToBoolean(); // How to fake ? override v8::Value::ToBoolean
*/
// while(so.AtEndOfStream != true) // It works. (without unary operator !)
// while(!so.AtEndOfStream) // It does not work.
while(!so.AtEndOfStream._) // *** It works. oops!
callback(so.ReadLine());
console.log('code = ' + stat.ExitCode);
}
var cmd = 'reg query "HKLM\\Software\\Microsoft\\Internet Explorer"';
shellexec(sh, cmd, function(line){
if(line.match(/([^\s]*version[^\s]*)[\s]+([^\s]+)[\s]+([^\s]+)/ig))
console.log(RegExp.$1 + ',' + RegExp.$2 + ',' + RegExp.$3);
});
shellexec(sh, 'ipconfig.exe', function(line){ console.log(line); });
win32ole.print('writing to eventlog ...');
var name = 'node-win32ole (' + win32ole.VERSION + ') ';
// the first argument value
// 0: EVENTLOG_SUCCESS
// 1: EVENTLOG_ERROR_TYPE
// 2: EVENTLOG_WARNING_TYPE
// 4: EVENTLOG_INFORMATION_TYPE
// 8: EVENTLOG_AUDIT_SUCCESS
// 16: EVENTLOG_AUDIT_FAILURE
// the 3rd argument '.' means 'self computer name' to send LogEvent target
sh.LogEvent(4, name + 'installed', '.');
sh.LogEvent(2, name + 'warning test', '.');
sh.LogEvent(1, name + 'error test', '.');
sh.LogEvent(0, name + 'success', '.');
win32ole.print('ok\n');
console.log('completed');
};
try{
wsh_sample(outfile);
}catch(e){
console.log('*** exception cached ***\n' + e);
}