2015-03-13 10:34:41 +01:00
|
|
|
/*jshint wsh:true */
|
|
|
|
|
|
|
|
var
|
|
|
|
FSO_ForReading = 1, FSO_ForWriting = 2,
|
2015-03-27 12:25:59 +01:00
|
|
|
PS_MSG = 'Microsoft Windows PowerShell is required.\n' +
|
|
|
|
'https://technet.microsoft.com/en-us/library/hh847837.aspx',
|
|
|
|
|
|
|
|
input, fso, tty,
|
|
|
|
options = (function(conf) {
|
|
|
|
var options = {}, arg, args =// Array.prototype.slice.call(WScript.Arguments),
|
|
|
|
(function() {
|
|
|
|
var args = [], i, iLen;
|
|
|
|
for (i = 0, iLen = WScript.Arguments.length; i < iLen; i++)
|
|
|
|
{ args.push(WScript.Arguments(i)); }
|
|
|
|
return args;
|
|
|
|
})(),
|
|
|
|
confLc = {}, key;
|
|
|
|
|
|
|
|
function decodeDOS(arg) {
|
|
|
|
return arg.replace(/#(\d+);/g, function(str, charCode) {
|
|
|
|
return String.fromCharCode(+charCode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (key in conf) {
|
|
|
|
if (conf.hasOwnProperty(key))
|
|
|
|
{ confLc[key.toLowerCase()] = {key: key, type: conf[key]}; }
|
|
|
|
}
|
|
|
|
|
|
|
|
while (typeof(arg = args.shift()) === 'string') {
|
|
|
|
if (!(arg = (arg.match(/^\-+(.+)$/) || [])[1])) { continue; }
|
|
|
|
arg = arg.toLowerCase();
|
|
|
|
if (confLc[arg]) {
|
|
|
|
options[confLc[arg].key] =
|
|
|
|
confLc[arg].type === 'boolean' ? true :
|
|
|
|
confLc[arg].type === 'string' ? args.shift() : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (key in conf) {
|
|
|
|
if (conf.hasOwnProperty(key) && conf[key] === 'string') {
|
|
|
|
if (typeof options[key] !== 'string') { options[key] = ''; }
|
|
|
|
else if (options.encoded) { options[key] = decodeDOS(options[key]); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
})({
|
|
|
|
display: 'string',
|
|
|
|
noEchoBack: 'boolean',
|
|
|
|
mask: 'string',
|
|
|
|
keyIn: 'boolean',
|
|
|
|
encoded: 'boolean'
|
|
|
|
});
|
2015-03-13 10:34:41 +01:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if (!options.noEchoBack && !options.keyIn) {
|
|
|
|
if (options.display !== '') { writeTTY(options.display); }
|
|
|
|
input = readByCS();
|
|
|
|
} else if (options.noEchoBack && !options.keyIn && options.mask === '*') {
|
|
|
|
if (options.display !== '') { writeTTY(options.display); }
|
|
|
|
input = readByPW();
|
|
|
|
} else {
|
|
|
|
WScript.StdErr.WriteLine(PS_MSG);
|
|
|
|
WScript.Quit(1);
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
WScript.StdOut.Write('\'' + input + '\'');
|
2015-03-13 10:34:41 +01:00
|
|
|
|
|
|
|
WScript.Quit();
|
|
|
|
|
2015-03-24 12:37:20 +01:00
|
|
|
function writeTTY(text) {
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-24 12:37:20 +01:00
|
|
|
tty = tty || getFso().OpenTextFile('CONOUT$', FSO_ForWriting, true);
|
|
|
|
tty.Write(text);
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
2015-03-24 12:37:20 +01:00
|
|
|
WScript.StdErr.WriteLine('TTY Write Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-03-25 13:22:44 +01:00
|
|
|
WScript.Quit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readByCS() {
|
2015-03-24 12:37:20 +01:00
|
|
|
var text;
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-24 12:37:20 +01:00
|
|
|
text = getFso().OpenTextFile('CONIN$', FSO_ForReading).ReadLine();
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
2015-03-24 12:37:20 +01:00
|
|
|
WScript.StdErr.WriteLine('TTY Read Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-03-13 10:34:41 +01:00
|
|
|
WScript.Quit(1);
|
|
|
|
}
|
2015-03-24 12:37:20 +01:00
|
|
|
return text;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
// TTY must be STDIN that is not redirected and not piped.
|
2015-03-25 13:22:44 +01:00
|
|
|
function readByPW() {
|
2015-03-27 12:25:59 +01:00
|
|
|
var text;
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
text = WScript.CreateObject('ScriptPW.Password').GetPassword()
|
2015-03-13 10:34:41 +01:00
|
|
|
// Bug? Illegal data may be returned when user types before initializing.
|
|
|
|
.replace(/[\u4000-\u40FF]/g, function(chr) {
|
|
|
|
var charCode = chr.charCodeAt(0);
|
|
|
|
return charCode >= 0x4020 && charCode <= 0x407F ?
|
|
|
|
String.fromCharCode(charCode - 0x4000) : '';
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
WScript.StdErr.WriteLine('ScriptPW.Password Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-03-13 10:34:41 +01:00
|
|
|
WScript.Quit(1);
|
|
|
|
}
|
2015-03-24 12:37:20 +01:00
|
|
|
writeTTY('\n');
|
2015-03-27 12:25:59 +01:00
|
|
|
return text;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:37:20 +01:00
|
|
|
function getFso() {
|
|
|
|
if (!fso) { fso = new ActiveXObject('Scripting.FileSystemObject'); }
|
|
|
|
return fso;
|
|
|
|
}
|