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+
15+ $ErrorActionPreference = ' Stop'
16+ Set-StrictMode - Version 1
17+
18+ $intermedateDir = " $PSScriptRoot \obj"
19+ mkdir $intermedateDir - ErrorAction Ignore | Out-Null
20+
21+ $bootstrapper = " $intermedateDir \vs_enterprise1.exe"
22+ Invoke-WebRequest - Uri ' https://aka.ms/vs/15/release/vs_enterprise.exe' - OutFile $bootstrapper
23+
24+ $vsJson = " $PSScriptRoot \VsRequirements\vs.json"
25+ # no backslashes - this breaks the installer
26+ $vsInstallPath = " ${env: ProgramFiles(x86)} \Microsoft Visual Studio\2017\Enterprise"
27+ $arguments = @ (
28+ ' --installPath' , " `" $vsInstallPath `" " ,
29+ ' --in' , $vsJson ,
30+ ' --wait' ,
31+ ' --norestart' )
32+
33+ if ($Update ) {
34+ $arguments = , ' update' + $arguments
35+ }
36+ else {
37+ $arguments = , ' modify' + $arguments
38+ }
39+
40+ if ($Quiet ) {
41+ $arguments += ' --quiet'
42+ }
43+
44+ Write-Host " Running '$bootstrapper $arguments ' on $ ( hostname) "
45+ $process = Start-Process - FilePath $bootstrapper `
46+ - ArgumentList $arguments `
47+ - Verb runas `
48+ - PassThru `
49+ - ErrorAction Stop
50+ Write-Host " pid = $ ( $process.Id ) "
51+ Wait-Process - InputObject $process
52+ Write-Host " exit code = $ ( $process.ExitCode ) "
53+
54+ # https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio#error-codes
55+ if ($process.ExitCode -eq 3010 ) {
56+ Write-Warning " Agent $ ( hostname) requires restart to finish the VS update"
57+ }
58+ elseif ($process.ExitCode -eq 5007 ) {
59+ Write-Error " Operation was blocked - the computer does not meet the requirements"
60+ }
61+ elseif (($process.ExitCode -eq 5004 ) -or ($process.ExitCode -eq 1602 )) {
62+ Write-Error " Operation was canceled"
63+ }
64+ elseif ($process.ExitCode -ne 0 ) {
65+ Write-Error " Installation failed on $ ( hostname) for unknown reason"
66+ }
0 commit comments