-
Notifications
You must be signed in to change notification settings - Fork 867
/
Copy pathVisualStudioFunctions.ps1
63 lines (60 loc) · 2.86 KB
/
VisualStudioFunctions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function Get-VisualStudio {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet(15, 16, 17)]
[int]$MajorVersion
)
try {
# Query for the latest 15.*/16.*/17.* version.
#
# Note, the capability is registered as VisualStudio_15.0/VisualStudio_16.0/VisualStudio_17.0 however the actual
# version may something like 15.2/16.2.
$preReleaseFlag = [string]::Empty;
if ($env:IncludePrereleaseVersions -eq $true)
{
$preReleaseFlag = "-prerelease"
}
Write-Host "Getting latest Visual Studio $MajorVersion setup instance."
$output = New-Object System.Text.StringBuilder
Write-Host "& $PSScriptRoot\..\..\..\externals\vswhere\vswhere.exe -version '[$MajorVersion.0,$($MajorVersion+1).0)' -latest $($preReleaseFlag) -format json"
& $PSScriptRoot\..\..\..\externals\vswhere\vswhere.exe -version "[$MajorVersion.0,$($MajorVersion+1).0)" -latest $preReleaseFlag -format json 2>&1 |
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
Write-Host "STDERR: $($_.Exception.Message)"
}
else {
Write-Host $_
$null = $output.AppendLine($_)
}
}
Write-Host "Exit code: $LASTEXITCODE"
if ($LASTEXITCODE -eq 0) {
$instance = (ConvertFrom-Json -InputObject $output.ToString()) |
Select-Object -First 1
if (!$instance) {
Write-Host "Getting latest BuildTools $MajorVersion setup instance."
$output = New-Object System.Text.StringBuilder
Write-Host "& $PSScriptRoot\..\..\..\externals\vswhere\vswhere.exe -version '[$MajorVersion.0,$($MajorVersion+1).0)' -products Microsoft.VisualStudio.Product.BuildTools -latest $preReleaseFlag -format json"
& $PSScriptRoot\..\..\..\externals\vswhere\vswhere.exe -version "[$MajorVersion.0,$($MajorVersion+1).0)" -products Microsoft.VisualStudio.Product.BuildTools -latest $preReleaseFlag -format json 2>&1 |
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
Write-Host "STDERR: $($_.Exception.Message)"
}
else {
Write-Host $_
$null = $output.AppendLine($_)
}
}
Write-Host "Exit code: $LASTEXITCODE"
if ($LASTEXITCODE -eq 0) {
$instance = (ConvertFrom-Json -InputObject $output.ToString()) |
Select-Object -First 1
}
}
return $instance
}
} catch {
Write-Host ($_ | Out-String)
}
}