-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscriptlib.js
152 lines (124 loc) · 4.06 KB
/
cscriptlib.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Wiederverwendare Funktionen in der Automation (cscript //e:jscript)
// Einen Befehl ausführen (mittels "exec")
// stdin, path und environment params können übergeben werden
// Rückgabe: stdout
function executeCommand( opt ) {
var sh = WScript.CreateObject("Wscript.Shell");
var stdout = "", stderr = "";
if (opt.path) sh.CurrentDirectory = opt.path;
if (opt.param) {
setProcessEnv(opt.param);
}
// java or javac: Shut off HP Quality Center's environment pollution
if (opt.cmd.match(/^java/)) {
if (typeof JAVA_HOME == "string") opt.cmd = '"' + opt.cmd.replace(/^java\b/,JAVA_HOME) + '"';
setProcessEnv( getParamsToShutOffQualityCenterIntrusion() );
}
var process = sh.exec( opt.cmd );
if (opt.input) {
process.StdIn.Write(opt.input);
process.StdIn.Close();
}
do {
WScript.sleep( 100 );
stdout += readFromTo( process.StdOut );
stderr += readFromTo( process.StdErr );
} while (!process.status);
stdout += readFromTo( process.StdOut );
stderr += readFromTo( process.StdErr );
if (process.ExitCode != 0 || !opt.ignoreStdErr && stderr.match(/\S/)) {
var message = "Fehler bei Ausführung von Kommando:\n" +
opt.cmd + "\n";
if (process.ExitCode != 0) {
message +=
"Abgebrochen mit Returncode <> 0 (" + process.ExitCode + ")\n";
}
else {
message +=
"Abgebrochen mit nichtleerem stderr\n"
}
message += "stderr = '\n" + stderr + "'\n";
throw message;
}
return stdout.replace(/\s*$/,"");
function readFromTo( channel ) {
return channel.AtEndOfStream ? "" : channel.ReadAll();
}
function setProcessEnv( param ) {
var v = sh.Environment("PROCESS"), name;
for (name in param) {
// This is possible only for host objects: A function's return value as lvalue
v(name) = param[name];
}
}
function getProcessEnv( name ) {
var v = sh.Environment("PROCESS");
return v(name);
}
function getParamsToShutOffQualityCenterIntrusion() {
var params = {
JAVA_TOOL_OPTIONS:'',
_JAVA_OPTIONS:''
};
if (typeof JAVA_HOME == "string") params.JAVA_HOME = JAVA_HOME;
return params;
}
}
// Umgebungsvariable ermitteln
var getenv = (function() {
var sh = WScript.CreateObject("Wscript.Shell");
var v = sh.Environment("PROCESS");
return function(name) {
return v(name);
}
})();
// Read a file completely into a string
function readFile( filename ) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fs = fso.OpenTextFile( filename, 1 );
return fs.ReadAll();
}
// SendMail
// I tried to use the built-in ActiveX object CDO.Message, but couldn't get rid of the erroe
// CDO.Message.1: Der "SendUsing"-Konfigurationswert ist ungültig.
// This message came even when I passed the Configuration.Fields parameters as specified
// Finally, I used this simple tool, http://caspian.dotconf.net/menu/Software/SendEmail/
// which immediately worked as expected
function sendMail(opts) {
function MailOptions() {
this.options = [];
}
MailOptions.prototype.addOptionArray = function(optArray) {
var options = this;
optArray.each( function() {
if (this[1] !== undefined) {
options.add(this[0],this[1]);
}
});
};
MailOptions.prototype.add = function( option, value ) {
this.options.push({ option:option, value:value });
};
MailOptions.prototype.get = function() {
var s = [];
this.options.each( function() {
s.push(this.option + ' ' + this.value);
});
return s.join(' ');
};
var options = new MailOptions();
options.addOptionArray([
[ '-f', opts.from],
[ '-t', opts.to],
[ '-s', opts.server],
[ '-u', opts.subject]]);
var exe = 'sendEmail.exe';
if (opts.exePath) {
exe = opts.exePath + exe;
}
var result = executeCommand({
cmd:'"'+exe+'" ' + options.get( ),
input: opts.body
})
return result;
}