Skip to content

Instantly share code, notes, and snippets.

@jonathanrodriguezs
Created June 21, 2020 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanrodriguezs/ad47c453341395584614c00cc3c8f709 to your computer and use it in GitHub Desktop.
Save jonathanrodriguezs/ad47c453341395584614c00cc3c8f709 to your computer and use it in GitHub Desktop.
Promisified powershell child process
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