InstallVisualStudio.ps1 7.0 KB

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