forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallVisualStudio.ps1
182 lines (159 loc) · 6.38 KB
/
InstallVisualStudio.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<#
.SYNOPSIS
Installs or updates Visual Studio on a local developer machine.
.DESCRIPTION
This installs Visual Studio along with all the workloads required to contribute to this repository.
.PARAMETER Edition
Selects which 'offering' of Visual Studio to install. Must be one of these values:
BuildTools
Community
Professional
Enterprise (the default)
.PARAMETER Channel
Selects which channel of Visual Studio to install. Must be one of these values:
Release (the default)
Preview
.PARAMETER InstallPath
The location on disk where Visual Studio should be installed or updated. Default path is location of latest
existing installation of the specified edition, if any. If that VS edition is not currently installed, default
path is '${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\`$Edition".
.PARAMETER Passive
Run the installer without requiring interaction.
.PARAMETER Quiet
Run the installer without UI and wait for installation to complete.
.LINK
https://visualstudio.com
https://github.com/aspnet/AspNetCore/blob/master/docs/BuildFromSource.md
.EXAMPLE
To install VS 2019 Enterprise, run this command in PowerShell:
.\InstallVisualStudio.ps1
#>
param(
[ValidateSet('BuildTools','Community', 'Professional', 'Enterprise')]
[string]$Edition = 'Enterprise',
[ValidateSet('Release', 'Preview')]
[string]$Channel = 'Release',
[string]$InstallPath,
[switch]$Passive,
[switch]$Quiet
)
if ($env:TF_BUILD) {
Write-Error 'This script is not intended for use on CI. It is only meant to be used to install a local developer environment. If you need to change Visual Studio requirements in CI agents, contact the @aspnet/build team.'
exit 1
}
if ($Passive -and $Quiet) {
Write-Host -ForegroundColor Red "Error: The -Passive and -Quiet options cannot be used together."
Write-Host -ForegroundColor Red "Run ``Get-Help $PSCommandPath`` for more details."
exit 1
}
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 1
$intermedateDir = "$PSScriptRoot\obj"
mkdir $intermedateDir -ErrorAction Ignore | Out-Null
$bootstrapper = "$intermedateDir\vsinstaller.exe"
$ProgressPreference = 'SilentlyContinue' # Workaround PowerShell/PowerShell#2138
$channelUri = "https://aka.ms/vs/16/release"
$responseFileName = "vs"
if ("$Edition" -eq "BuildTools") {
$responseFileName += ".buildtools"
}
if ("$Channel" -eq "Preview") {
$responseFileName += ".preview"
$channelUri = "https://aka.ms/vs/16/pre"
}
$responseFile = "$PSScriptRoot\$responseFileName.json"
$channelId = (Get-Content $responseFile | ConvertFrom-Json).channelId
$bootstrapperUri = "$channelUri/vs_$($Edition.ToLowerInvariant()).exe"
Write-Host "Downloading Visual Studio 2019 $Edition ($Channel) bootstrapper from $bootstrapperUri"
Invoke-WebRequest -Uri $bootstrapperUri -OutFile $bootstrapper
$productId = "Microsoft.VisualStudio.Product.$Edition"
if (-not $InstallPath) {
$vsWhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vsWhere)
{
$installations = & $vsWhere -version '[16,17)' -format json -sort -prerelease -products $productId | ConvertFrom-Json
foreach ($installation in $installations) {
Write-Host "Found '$($installation.installationName)' in '$($installation.installationPath)', channel = '$($installation.channelId)'"
if ($installation.channelId -eq $channelId) {
$InstallPath = $installation.installationPath
break
}
}
}
}
if (-not $InstallPath) {
if ("$Channel" -eq "Preview") {
$InstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\${Edition}_Pre"
} else {
$InstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\$Edition"
}
}
# no backslashes - this breaks the installer
$InstallPath = $InstallPath.TrimEnd('\')
[string[]] $arguments = @()
if (Test-path $InstallPath) {
$arguments += 'modify'
}
$arguments += `
'--productId', $productId, `
'--installPath', "`"$InstallPath`"", `
'--in', "`"$responseFile`"", `
'--norestart'
if ($Passive) {
$arguments += '--passive'
}
if ($Quiet) {
$arguments += '--quiet', '--wait'
}
Write-Host
Write-Host "Installing Visual Studio 2019 $Edition ($Channel)" -f Magenta
Write-Host
Write-Host "Running '$bootstrapper $arguments'"
foreach ($i in 0, 1, 2) {
if ($i -ne 0) {
Write-Host "Retrying..."
}
$process = Start-Process -FilePath "$bootstrapper" -ArgumentList $arguments -ErrorAction Continue -PassThru `
-RedirectStandardError "$intermedateDir\errors.txt" -Verbose -Wait
Write-Host "Exit code = $($process.ExitCode)."
if ($process.ExitCode -eq 0) {
break
} else {
# https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio#error-codes
if ($process.ExitCode -eq 3010) {
Write-Host -ForegroundColor Red "Error: Installation requires restart to finish the VS update."
break
}
elseif ($process.ExitCode -eq 5007) {
Write-Host -ForegroundColor Red "Error: Operation was blocked - the computer does not meet the requirements."
break
}
elseif (($process.ExitCode -eq 5004) -or ($process.ExitCode -eq 1602)) {
Write-Host -ForegroundColor Red "Error: Operation was canceled."
}
else {
Write-Host -ForegroundColor Red "Error: Installation failed for an unknown reason."
}
Write-Host
Write-Host "Errors:"
Get-Content "$intermedateDir\errors.txt" | Write-Warning
Write-Host
Get-ChildItem $env:Temp\dd_bootstrapper_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
Write-Host "${_}:"
Get-Content "$_"
Write-Host
}
$clientLogs = Get-ChildItem $env:Temp\dd_client_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
Write-Host "${_}:"
Get-Content "$_"
Write-Host
}
$setupLogs = Get-ChildItem $env:Temp\dd_setup_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
Write-Host "${_}:"
Get-Content "$_"
Write-Host
}
}
}
Remove-Item "$intermedateDir\errors.txt" -errorAction SilentlyContinue
exit $process.ExitCode