1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var
|
12 | FSO_ForReading = 1, FSO_ForWriting = 2,
|
13 | PS_MSG = 'Microsoft Windows PowerShell is required.' +
|
14 | ' https://technet.microsoft.com/en-us/library/hh847837.aspx',
|
15 |
|
16 | input = '', fso, tty,
|
17 | options = (function(conf) {
|
18 | var options = {}, arg, args =
|
19 | (function() {
|
20 | var args = [], i, iLen;
|
21 | for (i = 0, iLen = WScript.Arguments.length; i < iLen; i++)
|
22 | { args.push(WScript.Arguments(i)); }
|
23 | return args;
|
24 | })(),
|
25 | confLc = {}, key;
|
26 |
|
27 | function decodeArg(arg) {
|
28 | return arg.replace(/#(\d+);/g, function(str, charCode) {
|
29 | return String.fromCharCode(+charCode);
|
30 | });
|
31 | }
|
32 |
|
33 | for (key in conf) {
|
34 | if (conf.hasOwnProperty(key))
|
35 | { confLc[key.toLowerCase()] = {key: key, type: conf[key]}; }
|
36 | }
|
37 |
|
38 | while (typeof(arg = args.shift()) === 'string') {
|
39 | if (!(arg = (arg.match(/^\-+(.+)$/) || [])[1])) { continue; }
|
40 | arg = arg.toLowerCase();
|
41 | if (confLc[arg]) {
|
42 | options[confLc[arg].key] =
|
43 | confLc[arg].type === 'boolean' ? true :
|
44 | confLc[arg].type === 'string' ? args.shift() : null;
|
45 | }
|
46 | }
|
47 | for (key in conf) {
|
48 | if (conf.hasOwnProperty(key) && conf[key] === 'string') {
|
49 | if (typeof options[key] !== 'string') { options[key] = ''; }
|
50 | else { options[key] = decodeArg(options[key]); }
|
51 | }
|
52 | }
|
53 | return options;
|
54 | })({
|
55 | display: 'string',
|
56 | displayOnly: 'boolean',
|
57 | keyIn: 'boolean',
|
58 | hideEchoBack: 'boolean',
|
59 | mask: 'string'
|
60 | });
|
61 |
|
62 | if (!options.hideEchoBack && !options.keyIn) {
|
63 | if (options.display) { writeTTY(options.display); }
|
64 | if (!options.displayOnly) { input = readByFSO(); }
|
65 | } else if (options.hideEchoBack && !options.keyIn && !options.mask) {
|
66 | if (options.display) { writeTTY(options.display); }
|
67 | if (!options.displayOnly) { input = readByPW(); }
|
68 | } else {
|
69 | WScript.StdErr.WriteLine(PS_MSG);
|
70 | WScript.Quit(1);
|
71 | }
|
72 |
|
73 | WScript.StdOut.Write('\'' + input + '\'');
|
74 |
|
75 | WScript.Quit();
|
76 |
|
77 | function writeTTY(text) {
|
78 | try {
|
79 | tty = tty || getFso().OpenTextFile('CONOUT$', FSO_ForWriting, true);
|
80 | tty.Write(text);
|
81 | } catch (e) {
|
82 | WScript.StdErr.WriteLine('TTY Write Error: ' + e.number +
|
83 | '\n' + e.description + '\n' + PS_MSG);
|
84 | WScript.Quit(e.number || 1);
|
85 | }
|
86 | }
|
87 |
|
88 | function readByFSO() {
|
89 | var text;
|
90 | try {
|
91 | text = getFso().OpenTextFile('CONIN$', FSO_ForReading).ReadLine();
|
92 | } catch (e) {
|
93 | WScript.StdErr.WriteLine('TTY Read Error: ' + e.number +
|
94 | '\n' + e.description + '\n' + PS_MSG);
|
95 | WScript.Quit(e.number || 1);
|
96 | }
|
97 | return text;
|
98 | }
|
99 |
|
100 |
|
101 | function readByPW() {
|
102 | var text;
|
103 | try {
|
104 | text = WScript.CreateObject('ScriptPW.Password').GetPassword()
|
105 |
|
106 | .replace(/[\u4000-\u40FF]/g, function(chr) {
|
107 | var charCode = chr.charCodeAt(0);
|
108 | return charCode >= 0x4020 && charCode <= 0x407F ?
|
109 | String.fromCharCode(charCode - 0x4000) : '';
|
110 | });
|
111 | } catch (e) {
|
112 | WScript.StdErr.WriteLine('ScriptPW.Password Error: ' + e.number +
|
113 | '\n' + e.description + '\n' + PS_MSG);
|
114 | WScript.Quit(e.number || 1);
|
115 | }
|
116 | writeTTY('\n');
|
117 | return text;
|
118 | }
|
119 |
|
120 | function getFso() {
|
121 | if (!fso) { fso = new ActiveXObject('Scripting.FileSystemObject'); }
|
122 | return fso;
|
123 | }
|