powerShell optimizatio error handling

This commit is contained in:
Sebastian Hildebrandt 2019-02-05 11:30:30 +01:00
parent 78f4ef5046
commit 8df7d8a93a

View File

@ -218,24 +218,10 @@ 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'
@ -246,20 +232,24 @@ function powerShell(cmd) {
result = result + data.toString('utf8');
});
child.stderr.on('data', function (data) {
dispose(child).then(resolve(result));
child.kill();
resolve(result);
});
child.on('close', function () {
dispose(child).then(resolve(result));
;
child.kill();
resolve(result);
});
child.on('error', function () {
dispose(child).then(resolve(result));
child.kill();
resolve(result);
});
try {
child.stdin.write(cmd + os.EOL);
child.stdin.write('exit' + os.EOL);
child.stdin.end()
} catch (e) {
dispose(child).then(resolve(result));
child.kill();
resolve(result);
}
} else {
resolve(result);
@ -270,7 +260,6 @@ function powerShell(cmd) {
});
});
}
function getCodepage() {
if (_windows) {
if (!codepage) {