-
Notifications
You must be signed in to change notification settings - Fork 1
/
powershell.js
41 lines (36 loc) · 1.35 KB
/
powershell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use strict";
const cp = require('child_process');
function run(path, args) {
return new Promise(function (resolve, reject) {
let stdout = '', stderr = '';
let proc = cp.spawn(path, args);
proc.on('error', reject);
proc.stdout.on('data', function (c) { stdout += c.toString(); });
proc.stderr.on('data', function (c) { stderr += c.toString(); });
proc.on('exit', function (code, signal) {
if (code !== 0) reject(new Error(stderr || ('child process exited with error code ' + code)))
resolve(stdout);
})
});
}
function runPS(cmd) {
return run('powershell', [
'-NonInteractive',
'-ExecutionPolicy', 'Bypass',
'-Command', cmd
]);
}
function runElevatedPS(cmd) {
const innerPSArguments = ['-ExecutionPolicy', 'Bypass', '-NonInteractive', '-Command', cmd].map(e => quote(e, true));
const psCommand = ['Exit', '(Start-Process', '-WindowStyle', 'Hidden', '-PassThru', '-Wait', 'powershell', '-Verb', 'runas', '-ArgumentList @(', innerPSArguments.join(','), ')).ExitCode'].join(' ');
return runPS(psCommand);
}
function quote(s, quote) {
let r = s.replace(/'/g, "''");
return quote === true ? "'" + r + "'" : r;
}
module.exports = {
quote: quote,
run: runPS,
runElevated: runElevatedPS
};