From eabd6c0701b00b0cb954d5dd64e3cf17922e6595 Mon Sep 17 00:00:00 2001 From: Tatsunori Uchino Date: Mon, 9 Dec 2019 02:50:44 +0900 Subject: [PATCH] Use vswhere as defaultVS2017+ contain an utility named vswhere, which discovers installed VS's.VS2019 abolished the registry that contains the infomation of the installed version. --- Code/Import-VisualStudioEnvironment.ps1 | 55 ++++++++++++++++++------- Code/Internal.ps1 | 28 +++++++++---- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/Code/Import-VisualStudioEnvironment.ps1 b/Code/Import-VisualStudioEnvironment.ps1 index 0243d38..fc21b4c 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,32 @@ 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 +131,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 }