powerShell optimizatio error handling
This commit is contained in:
parent
39f5aa6d29
commit
78f4ef5046
38
lib/util.js
38
lib/util.js
@ -217,39 +217,55 @@ function getWmic() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function powerShell(cmd) {
|
function powerShell(cmd) {
|
||||||
|
|
||||||
|
// helper function
|
||||||
|
function dispose(child) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
child.stdin.write('exit' + os.EOL);
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
|
child.stdin.end()
|
||||||
|
child.kill();
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
process.nextTick(() => {
|
||||||
|
|
||||||
|
try {
|
||||||
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
|
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
|
||||||
stdio: 'pipe'
|
stdio: 'pipe'
|
||||||
});
|
});
|
||||||
|
|
||||||
if (child) {
|
if (child && child.pid) {
|
||||||
child.stdout.on('data', function (data) {
|
child.stdout.on('data', function (data) {
|
||||||
result = result + data.toString('utf8');
|
result = result + data.toString('utf8');
|
||||||
});
|
});
|
||||||
child.stderr.on('data', function (data) {
|
child.stderr.on('data', function (data) {
|
||||||
child.kill();
|
dispose(child).then(resolve(result));
|
||||||
reject(data);
|
|
||||||
});
|
});
|
||||||
child.on('close', function () {
|
child.on('close', function () {
|
||||||
child.kill();
|
dispose(child).then(resolve(result));
|
||||||
resolve(result);
|
;
|
||||||
});
|
});
|
||||||
child.on('error', function () {
|
child.on('error', function () {
|
||||||
child.kill();
|
dispose(child).then(resolve(result));
|
||||||
resolve(result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
child.stdin.write(cmd + '\n');
|
child.stdin.write(cmd + os.EOL);
|
||||||
child.stdin.end();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(result)
|
dispose(child).then(resolve(result));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
reject(result);
|
resolve(result);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
resolve(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user