install_vs.ps1 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <#
  2. .SYNOPSIS
  3. Installs or updates Visual Studio on a local developer machine
  4. .PARAMETER Update
  5. Update VS to latest version instead of modifying the installation to include new workloads.
  6. .PARAMETER Quiet
  7. Whether to run installer in the background
  8. #>
  9. [CmdletBinding(DefaultParameterSetName = 'Default')]
  10. param(
  11. [switch]$Update,
  12. [switch]$Quiet
  13. )
  14. $ErrorActionPreference = 'Stop'
  15. Set-StrictMode -Version 1
  16. $intermedateDir = "$PSScriptRoot\obj"
  17. mkdir $intermedateDir -ErrorAction Ignore | Out-Null
  18. $bootstrapper = "$intermedateDir\vs_enterprise1.exe"
  19. Invoke-WebRequest -Uri 'https://aka.ms/vs/15/release/vs_enterprise.exe' -OutFile $bootstrapper
  20. $vsJson = "$PSScriptRoot\VsRequirements\vs.json"
  21. # no backslashes - this breaks the installer
  22. $vsInstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise"
  23. $arguments = @(
  24. '--installPath', "`"$vsInstallPath`"",
  25. '--in', $vsJson,
  26. '--wait',
  27. '--norestart')
  28. if ($Update) {
  29. $arguments = ,'update' + $arguments
  30. }
  31. else {
  32. $arguments = ,'modify' + $arguments
  33. }
  34. if ($Quiet) {
  35. $arguments += '--quiet'
  36. }
  37. Write-Host "Running '$bootstrapper $arguments' on $(hostname)"
  38. $process = Start-Process -FilePath $bootstrapper `
  39. -ArgumentList $arguments `
  40. -Verb runas `
  41. -PassThru `
  42. -ErrorAction Stop
  43. Write-Host "pid = $($process.Id)"
  44. Wait-Process -InputObject $process
  45. Write-Host "exit code = $($process.ExitCode)"
  46. # https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio#error-codes
  47. if ($process.ExitCode -eq 3010) {
  48. Write-Warning "Agent $(hostname) requires restart to finish the VS update"
  49. }
  50. elseif ($process.ExitCode -eq 5007) {
  51. Write-Error "Operation was blocked - the computer does not meet the requirements"
  52. }
  53. elseif (($process.ExitCode -eq 5004) -or ($process.ExitCode -eq 1602)) {
  54. Write-Error "Operation was canceled"
  55. }
  56. elseif ($process.ExitCode -ne 0) {
  57. Write-Error "Installation failed on $(hostname) for unknown reason"
  58. }