diff --git a/lib/find-visualstudio.js b/lib/find-visualstudio.js index 8a5cfc1ea9..a99572328d 100644 --- a/lib/find-visualstudio.js +++ b/lib/find-visualstudio.js @@ -126,23 +126,40 @@ VisualStudioFinder.prototype = { // Invoke the PowerShell script to get information about Visual Studio 2017 // or newer installations findVisualStudio2017OrNewer: function findVisualStudio2017OrNewer (cb) { - var ps = path.join(process.env.SystemRoot, 'System32', - 'WindowsPowerShell', 'v1.0', 'powershell.exe') - var csFile = path.join(__dirname, 'Find-VisualStudio.cs') - var psArgs = [ - '-ExecutionPolicy', - 'Unrestricted', - '-NoProfile', - '-Command', - '&{Add-Type -Path \'' + csFile + '\';' + '[VisualStudioConfiguration.Main]::PrintJson()}' - ] + const ps = path.join(process.env.SystemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe') + const csFile = path.join(__dirname, 'Find-VisualStudio.cs') + const basePsArgs = ['-ExecutionPolicy', 'Unrestricted', '-NoProfile', '-Command'] + const findLanguageModePsCmd = + '&{ try { $ExecutionContext.SessionState.LanguageMode } catch { try { (Get-PSSessionConfiguration -Name Test).LanguageMode } catch { \'error\' } } }' + const psArgs = [...basePsArgs, findLanguageModePsCmd] this.log.silly('Running', ps, psArgs) - var child = execFile(ps, psArgs, { encoding: 'utf8' }, + const childFindPsLanguageMode = execFile( + ps, + psArgs, + { encoding: 'utf8' }, (err, stdout, stderr) => { - this.parseData(err, stdout, stderr, cb) + this.log.silly('PS stderr = %j', stderr) + if (err) { + this.log.silly('PS err = %j', err && (err.stack || err)) + this.addLog( + 'could not determine PowerShell LanguageMode, try re-running with \'--loglevel silly\' for more details') + return cb(null) + } + const findVscDataPsCmd = `&{Add-Type -Path '${csFile}';[VisualStudioConfiguration.Main]::PrintJson()}` + const psArgs = [...basePsArgs, findVscDataPsCmd] + // the Add-Type command requires powershell to run with languageMode == FullLanguage + // Because languageMode feature is not introduced yet in powershell v2, we can try to use it. + // If powershell v2 is not installed (a problem only if languageMode is not FullLanguage), then it is not + // possible to use feature Add-Type. + if (stdout.toString('utf8').trim().toLowerCase() !== 'fulllanguage') { + psArgs.unshift('-Version', '2') + } + this.log.silly('Running', ps, psArgs) + const child = execFile(ps, psArgs, { encoding: 'utf8' }, (err, stdout, stderr) => this.parseData(err, stdout, stderr, cb)) + child.stdin.end() }) - child.stdin.end() + childFindPsLanguageMode.stdin.end() }, // Parse the output of the PowerShell script and look for an installation