Created
June 21, 2020 06:39
-
-
Save jonathanrodriguezs/ad47c453341395584614c00cc3c8f709 to your computer and use it in GitHub Desktop.
Promisified powershell child process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function powershell(commands) { | |
return new Promise(function (resolve, reject) { | |
const spawn = require("child_process").spawn | |
const child = spawn("powershell.exe", ["-Command", "-"]) | |
const result = [], errors = [] | |
child.stdout.on("data", data => { | |
result.push(data.toString()) | |
}) | |
child.stderr.on("data", data => { | |
errors.push(data.toString()) | |
}) | |
child.on("exit", () => { | |
console.log("Powershell Script finished") | |
if (errors.length === 0) resolve(result) | |
else reject(errors) | |
}) | |
commands.forEach(cmd => { | |
child.stdin.write(cmd + '\n') | |
}) | |
child.stdin.end() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment