InstallVisualStudio.ps1 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <#
  2. .SYNOPSIS
  3. Installs or updates Visual Studio on a local developer machine.
  4. .DESCRIPTION
  5. This installs Visual Studio along with all the workloads required to contribute to this repository.
  6. .PARAMETER Edition
  7. Selects which 'offering' of Visual Studio to install. Must be one of these values:
  8. BuildTools
  9. Community
  10. Professional
  11. Enterprise (the default)
  12. .PARAMETER Channel
  13. Selects which channel of Visual Studio to install. Must be one of these values:
  14. Release (the default)
  15. Preview
  16. .PARAMETER InstallPath
  17. The location on disk where Visual Studio should be installed or updated. Default path is location of latest
  18. existing installation of the specified edition, if any. If that VS edition is not currently installed, default
  19. path is '${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\`$Edition".
  20. .PARAMETER Passive
  21. Run the installer without requiring interaction.
  22. .PARAMETER Quiet
  23. Run the installer without UI and wait for installation to complete.
  24. .LINK
  25. https://visualstudio.com
  26. https://github.com/dotnet/aspnetcore/blob/main/docs/BuildFromSource.md
  27. .EXAMPLE
  28. To install VS 2019 Enterprise, run this command in PowerShell:
  29. .\InstallVisualStudio.ps1
  30. #>
  31. param(
  32. [ValidateSet('BuildTools','Community', 'Professional', 'Enterprise')]
  33. [string]$Edition = 'Enterprise',
  34. [ValidateSet('Release', 'Preview')]
  35. [string]$Channel = 'Release',
  36. [string]$InstallPath,
  37. [switch]$Passive,
  38. [switch]$Quiet
  39. )
  40. if ($env:TF_BUILD) {
  41. 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.'
  42. exit 1
  43. }
  44. if ($Passive -and $Quiet) {
  45. Write-Host -ForegroundColor Red "Error: The -Passive and -Quiet options cannot be used together."
  46. Write-Host -ForegroundColor Red "Run ``Get-Help $PSCommandPath`` for more details."
  47. exit 1
  48. }
  49. $ErrorActionPreference = 'Stop'
  50. Set-StrictMode -Version 1
  51. $intermedateDir = "$PSScriptRoot\obj"
  52. mkdir $intermedateDir -ErrorAction Ignore | Out-Null
  53. $bootstrapper = "$intermedateDir\vsinstaller.exe"
  54. $ProgressPreference = 'SilentlyContinue' # Workaround PowerShell/PowerShell#2138
  55. $channelUri = "https://aka.ms/vs/16/release"
  56. $responseFileName = "vs"
  57. if ("$Edition" -eq "BuildTools") {
  58. $responseFileName += ".buildtools"
  59. }
  60. if ("$Channel" -eq "Preview") {
  61. $responseFileName += ".preview"
  62. $channelUri = "https://aka.ms/vs/16/pre"
  63. }
  64. $responseFile = "$PSScriptRoot\$responseFileName.json"
  65. $channelId = (Get-Content $responseFile | ConvertFrom-Json).channelId
  66. $bootstrapperUri = "$channelUri/vs_$($Edition.ToLowerInvariant()).exe"
  67. Write-Host "Downloading Visual Studio 2019 $Edition ($Channel) bootstrapper from $bootstrapperUri"
  68. Invoke-WebRequest -Uri $bootstrapperUri -OutFile $bootstrapper
  69. $productId = "Microsoft.VisualStudio.Product.$Edition"
  70. if (-not $InstallPath) {
  71. $vsWhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
  72. if (Test-Path $vsWhere)
  73. {
  74. $installations = & $vsWhere -version '[16,17)' -format json -prerelease -products $productId | ConvertFrom-Json |Sort-Object -Descending -Property installationVersion, installDate
  75. foreach ($installation in $installations) {
  76. Write-Host "Found '$($installation.installationName)' in '$($installation.installationPath)', channel = '$($installation.channelId)'"
  77. if ($installation.channelId -eq $channelId) {
  78. $InstallPath = $installation.installationPath
  79. break
  80. }
  81. }
  82. }
  83. }
  84. if (-not $InstallPath) {
  85. if ("$Channel" -eq "Preview") {
  86. $InstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\${Edition}_Pre"
  87. } else {
  88. $InstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\$Edition"
  89. }
  90. }
  91. # no backslashes - this breaks the installer
  92. $InstallPath = $InstallPath.TrimEnd('\')
  93. [string[]] $arguments = @()
  94. if (Test-path $InstallPath) {
  95. $arguments += 'modify'
  96. }
  97. $arguments += `
  98. '--productId', $productId, `
  99. '--installPath', "`"$InstallPath`"", `
  100. '--in', "`"$responseFile`""
  101. if ($Passive) {
  102. $arguments += '--passive', '--norestart'
  103. }
  104. if ($Quiet) {
  105. $arguments += '--quiet', '--wait', '--norestart'
  106. }
  107. Write-Host
  108. Write-Host "Installing Visual Studio 2019 $Edition ($Channel)" -f Magenta
  109. Write-Host
  110. Write-Host "Running '$bootstrapper $arguments'"
  111. foreach ($i in 0, 1, 2) {
  112. if ($i -ne 0) {
  113. Write-Host "Retrying..."
  114. }
  115. $process = Start-Process -FilePath "$bootstrapper" -ArgumentList $arguments -ErrorAction Continue -PassThru `
  116. -RedirectStandardError "$intermedateDir\errors.txt" -Verbose -Wait
  117. Write-Host "Exit code = $($process.ExitCode)."
  118. if ($process.ExitCode -eq 0) {
  119. break
  120. } else {
  121. # https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio#error-codes
  122. if ($process.ExitCode -eq 3010) {
  123. Write-Host -ForegroundColor Red "Error: Installation requires restart to finish the VS update."
  124. break
  125. }
  126. elseif ($process.ExitCode -eq 5007) {
  127. Write-Host -ForegroundColor Red "Error: Operation was blocked - the computer does not meet the requirements."
  128. break
  129. }
  130. elseif (($process.ExitCode -eq 5004) -or ($process.ExitCode -eq 1602)) {
  131. Write-Host -ForegroundColor Red "Error: Operation was canceled."
  132. }
  133. else {
  134. Write-Host -ForegroundColor Red "Error: Installation failed for an unknown reason."
  135. }
  136. Write-Host
  137. Write-Host "Errors:"
  138. Get-Content "$intermedateDir\errors.txt" | Write-Warning
  139. Write-Host
  140. Get-ChildItem $env:Temp\dd_bootstrapper_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
  141. Write-Host "${_}:"
  142. Get-Content "$_"
  143. Write-Host
  144. }
  145. $clientLogs = Get-ChildItem $env:Temp\dd_client_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
  146. Write-Host "${_}:"
  147. Get-Content "$_"
  148. Write-Host
  149. }
  150. $setupLogs = Get-ChildItem $env:Temp\dd_setup_*.log |Sort-Object CreationTime -Descending |Select-Object -First 1 |% {
  151. Write-Host "${_}:"
  152. Get-Content "$_"
  153. Write-Host
  154. }
  155. }
  156. }
  157. Remove-Item "$intermedateDir\errors.txt" -errorAction SilentlyContinue
  158. exit $process.ExitCode