diff --git a/lib/util.js b/lib/util.js index 62320229..693ab9c5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -396,6 +396,17 @@ function powerShellProceedResults(data) { } function powerShellStart() { + // Reuse existing persisted process, if one is available + if (_psPersistent && _psChild && _psChild.pid){ + return; + } + + // If somehow _psPersistent is not in sync with _psChild, + // just kill it and start a new one. + if (!_psPersistent && _psChild && _psChild.pid) { + powerShellRelease(); + } + _psChild = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-Command', '-'], { stdio: 'pipe', windowsHide: true, @@ -403,25 +414,24 @@ function powerShellStart() { encoding: 'UTF-8', env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' }) }); - if (_psChild && _psChild.pid) { - _psPersistent = true; - _psChild.stdout.on('data', function (data) { - _psResult = _psResult + data.toString('utf8'); - if (data.indexOf(_psCmdSeperator) >= 0) { - powerShellProceedResults(_psResult); - _psResult = ''; - } - }); - _psChild.stderr.on('data', function () { - powerShellProceedResults(_psResult + _psError); - }); - _psChild.on('error', function () { - powerShellProceedResults(_psResult + _psError); - }); - _psChild.on('close', function () { - _psChild.kill(); - }); - } + + _psPersistent = true; + _psChild.stdout.on('data', function (data) { + _psResult = _psResult + data.toString('utf8'); + if (data.indexOf(_psCmdSeperator) >= 0) { + powerShellProceedResults(_psResult); + _psResult = ''; + } + }); + _psChild.stderr.on('data', function () { + powerShellProceedResults(_psResult + _psError); + }); + _psChild.on('error', function () { + powerShellProceedResults(_psResult + _psError); + }); + _psChild.on('close', function () { + _psChild.kill(); + }); } function powerShellRelease() {