diff --git a/Code/Import-VisualStudioEnvironment.ps1 b/Code/Import-VisualStudioEnvironment.ps1 index 0243d38..f0c6354 100644 --- a/Code/Import-VisualStudioEnvironment.ps1 +++ b/Code/Import-VisualStudioEnvironment.ps1 @@ -1,4 +1,4 @@ -#requires -version 5.0 +#requires -version 5.0 ############################################################################### # WintellectPowerShell Module # Copyright (c) 2010-2017 - John Robbins/Wintellect @@ -74,6 +74,10 @@ https://github.com/Wintellect/WintellectPowerShell [string]$AdditionalOptions = "" ) + # VS2017+ + $vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" + + # for VS2017 and older $versionSearchKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" if ([IntPtr]::size -ne 8) { @@ -87,20 +91,31 @@ https://github.com/Wintellect/WintellectPowerShell if ($VSVersion -eq 'Latest') { - # Find the largest number in the install lookup directory and that will - # be the latest version. - $biggest = 0.0 - Get-RegistryKeyPropertiesAndValues $versionSearchKey | - ForEach-Object { - if ([System.Convert]::ToDecimal($_.Property, [CultureInfo]::InvariantCulture) -gt ` - [System.Convert]::ToDecimal($biggest, [CultureInfo]::InvariantCulture)) - { - $biggest = $_.Property - $vsDirectory = $_.Value - } - } + if (Test-Path -PathType Leaf $vswherePath) + { + $latestVSInfo = & $vswherePath -latest -legacy -format json | ConvertFrom-Json + if ($latestVSInfo) + { + $usingVersion = [System.Convert]::ToDecimal(($latestVSInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture) + $vsDirectory = $latestVSInfo.installationPath + } + } + else { + # Find the largest number in the install lookup directory and that will + # be the latest version. + $biggest = 0.0 + Get-RegistryKeyPropertiesAndValues $versionSearchKey | + ForEach-Object { + if ([System.Convert]::ToDecimal($_.Property, [CultureInfo]::InvariantCulture) -gt ` + [System.Convert]::ToDecimal($biggest, [CultureInfo]::InvariantCulture)) + { + $biggest = $_.Property + $vsDirectory = $_.Value + } + } - $usingVersion = $biggest + $usingVersion = $biggest + } } else { @@ -115,7 +130,16 @@ https://github.com/Wintellect/WintellectPowerShell $usingVersion = [System.Convert]::ToDecimal($propVal, [CultureInfo]::InvariantCulture) - if (Test-PathReg -Path $versionSearchKey -Property $propVal) + if (Test-Path -PathType Leaf $vswherePath) + { + $vsInfo = & $vswherePath -version "[${usingVersion},$($usingVersion + 1))" -legacy -format json | ConvertFrom-Json + if ($vsInfo) + { + $usingVersion = [System.Convert]::ToDecimal(($vsInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture) + $vsDirectory = $vsInfo.installationPath + } + } + elseif (Test-PathReg -Path $versionSearchKey -Property $propVal) { $vsDirectory = (Get-ItemProperty -Path $versionSearchKey -WarningAction SilentlyContinue).$propVal } diff --git a/Code/Internal.ps1 b/Code/Internal.ps1 index 149af8c..3ff12c7 100644 --- a/Code/Internal.ps1 +++ b/Code/Internal.ps1 @@ -93,19 +93,33 @@ function Get-RegistryKeyPropertiesAndValues function LatestVSRegistryKeyVersion { + # 2017+ + $vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" + # 2017 and older $versionSearchKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" if ([IntPtr]::size -ne 8) { $versionSearchKey = "HKLM:\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" } $biggest = 0.0 - Get-RegistryKeyPropertiesAndValues $versionSearchKey | - ForEach-Object { - if ([System.Convert]::ToDecimal($_.Property) -gt [System.Convert]::ToDecimal($biggest)) - { - $biggest = $_.Property - } - } + if (Test-Path -PathType Leaf $vswherePath) + { + $latestVSInfo = & $vswherePath -latest -legacy -format json | ConvertFrom-Json + if ($latestVSInfo) + { + $biggest = [System.Convert]::ToDecimal(($latestVSInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture) + } + } + else + { + Get-RegistryKeyPropertiesAndValues $versionSearchKey | + ForEach-Object { + if ([System.Convert]::ToDecimal($_.Property) -gt [System.Convert]::ToDecimal($biggest)) + { + $biggest = $_.Property + } + } + } $biggest }