powerShell optimizatio error handling

This commit is contained in:
Sebastian Hildebrandt 2019-02-05 10:42:57 +01:00
parent 39f5aa6d29
commit 78f4ef5046

View File

@ -217,39 +217,55 @@ function getWmic() {
}
function powerShell(cmd) {
// helper function
function dispose(child) {
return new Promise((resolve, reject) => {
child.stdin.write('exit' + os.EOL);
process.nextTick(() => {
child.stdin.end()
child.kill();
resolve();
});
})
}
let result = '';
return new Promise((resolve, reject) => {
process.nextTick(() => {
try {
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
stdio: 'pipe'
});
if (child) {
if (child && child.pid) {
child.stdout.on('data', function (data) {
result = result + data.toString('utf8');
});
child.stderr.on('data', function (data) {
child.kill();
reject(data);
dispose(child).then(resolve(result));
});
child.on('close', function () {
child.kill();
resolve(result);
dispose(child).then(resolve(result));
;
});
child.on('error', function () {
child.kill();
resolve(result);
dispose(child).then(resolve(result));
});
try {
child.stdin.write(cmd + '\n');
child.stdin.end();
child.stdin.write(cmd + os.EOL);
} catch (e) {
reject(result)
dispose(child).then(resolve(result));
}
} else {
reject(result);
resolve(result);
}
} catch (e) {
resolve(result);
}
});
});