Skip to content

Commit

Permalink
Use vswhere as defaultVS2017+ contain an utility named vswhere, which…
Browse files Browse the repository at this point in the history
… discovers installed VS's.VS2019 abolished the registry that contains the infomation of the installed version.
  • Loading branch information
tats-u committed Dec 9, 2019
1 parent bc04d5d commit eabd6c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
55 changes: 40 additions & 15 deletions Code/Import-VisualStudioEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#requires -version 5.0
#requires -version 5.0
###############################################################################
# WintellectPowerShell Module
# Copyright (c) 2010-2017 - John Robbins/Wintellect
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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
}
Expand Down
28 changes: 21 additions & 7 deletions Code/Internal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit eabd6c0

Please sign in to comment.