Skip to content

Commit 0671e06

Browse files
committed
Use vswhere as default
VS2017+ contain an utility named vswhere, which discovers installed VS's. VS2019 abolished the registry that contains the infomation of the installed version.
1 parent bc04d5d commit 0671e06

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

Code/Import-VisualStudioEnvironment.ps1

+39-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#requires -version 5.0
1+
#requires -version 5.0
22
###############################################################################
33
# WintellectPowerShell Module
44
# Copyright (c) 2010-2017 - John Robbins/Wintellect
@@ -74,6 +74,10 @@ https://github.com/Wintellect/WintellectPowerShell
7474
[string]$AdditionalOptions = ""
7575
)
7676

77+
# VS2017+
78+
$vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
79+
80+
# for VS2017 and older
7781
$versionSearchKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7"
7882
if ([IntPtr]::size -ne 8)
7983
{
@@ -87,20 +91,31 @@ https://github.com/Wintellect/WintellectPowerShell
8791

8892
if ($VSVersion -eq 'Latest')
8993
{
90-
# Find the largest number in the install lookup directory and that will
91-
# be the latest version.
92-
$biggest = 0.0
93-
Get-RegistryKeyPropertiesAndValues $versionSearchKey |
94-
ForEach-Object {
95-
if ([System.Convert]::ToDecimal($_.Property, [CultureInfo]::InvariantCulture) -gt `
96-
[System.Convert]::ToDecimal($biggest, [CultureInfo]::InvariantCulture))
97-
{
98-
$biggest = $_.Property
99-
$vsDirectory = $_.Value
100-
}
101-
}
94+
if (Test-Path -PathType Leaf $vswherePath)
95+
{
96+
$latestVSInfo = & $vswherePath -latest -legacy -format json | ConvertFrom-Json
97+
if ($latestVSInfo)
98+
{
99+
$usingVersion = [System.Convert]::ToDecimal(($latestVSInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture)
100+
$vsDirectory = $latestVSInfo.installationPath
101+
}
102+
}
103+
else {
104+
# Find the largest number in the install lookup directory and that will
105+
# be the latest version.
106+
$biggest = 0.0
107+
Get-RegistryKeyPropertiesAndValues $versionSearchKey |
108+
ForEach-Object {
109+
if ([System.Convert]::ToDecimal($_.Property, [CultureInfo]::InvariantCulture) -gt `
110+
[System.Convert]::ToDecimal($biggest, [CultureInfo]::InvariantCulture))
111+
{
112+
$biggest = $_.Property
113+
$vsDirectory = $_.Value
114+
}
115+
}
102116

103-
$usingVersion = $biggest
117+
$usingVersion = $biggest
118+
}
104119
}
105120
else
106121
{
@@ -115,7 +130,16 @@ https://github.com/Wintellect/WintellectPowerShell
115130

116131
$usingVersion = [System.Convert]::ToDecimal($propVal, [CultureInfo]::InvariantCulture)
117132

118-
if (Test-PathReg -Path $versionSearchKey -Property $propVal)
133+
if (Test-Path -PathType Leaf $vswherePath)
134+
{
135+
$vsInfo = & $vswherePath -version "[${usingVersion},$($usingVersion + 1))" -legacy -format json | ConvertFrom-Json
136+
if ($vsInfo)
137+
{
138+
$usingVersion = [System.Convert]::ToDecimal(($vsInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture)
139+
$vsDirectory = $vsInfo.installationPath
140+
}
141+
}
142+
elseif (Test-PathReg -Path $versionSearchKey -Property $propVal)
119143
{
120144
$vsDirectory = (Get-ItemProperty -Path $versionSearchKey -WarningAction SilentlyContinue).$propVal
121145
}

Code/Internal.ps1

+21-7
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,33 @@ function Get-RegistryKeyPropertiesAndValues
9393

9494
function LatestVSRegistryKeyVersion
9595
{
96+
# 2017+
97+
$vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
98+
# 2017 and older
9699
$versionSearchKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7"
97100
if ([IntPtr]::size -ne 8)
98101
{
99102
$versionSearchKey = "HKLM:\SOFTWARE\Microsoft\VisualStudio\SxS\VS7"
100103
}
101104
$biggest = 0.0
102-
Get-RegistryKeyPropertiesAndValues $versionSearchKey |
103-
ForEach-Object {
104-
if ([System.Convert]::ToDecimal($_.Property) -gt [System.Convert]::ToDecimal($biggest))
105-
{
106-
$biggest = $_.Property
107-
}
108-
}
105+
if (Test-Path -PathType Leaf $vswherePath)
106+
{
107+
$latestVSInfo = & $vswherePath -latest -legacy -format json | ConvertFrom-Json
108+
if ($latestVSInfo)
109+
{
110+
$biggest = [System.Convert]::ToDecimal(($latestVSInfo.installationVersion -replace "^(\d+\.\d+)[^\d].*", "`$1"), [CultureInfo]::InvariantCulture)
111+
}
112+
}
113+
else
114+
{
115+
Get-RegistryKeyPropertiesAndValues $versionSearchKey |
116+
ForEach-Object {
117+
if ([System.Convert]::ToDecimal($_.Property) -gt [System.Convert]::ToDecimal($biggest))
118+
{
119+
$biggest = $_.Property
120+
}
121+
}
122+
}
109123

110124
$biggest
111125
}

0 commit comments

Comments
 (0)