|
| 1 | +param ( |
| 2 | + [Parameter(Mandatory=$true)] |
| 3 | + [string[]] $Components, |
| 4 | + |
| 5 | + [uri] $InstallerUri = "https://download.visualstudio.microsoft.com/download/pr/c4fef23e-cc45-4836-9544-70e213134bc8/1ee5717e9a1e05015756dff77eb27d554a79a6db91f2716d836df368381af9a1/vs_Enterprise.exe", |
| 6 | + |
| 7 | + [string] $VsInstaller = "${env:System_DefaultWorkingDirectory}\vs_Enterprise.exe", |
| 8 | + |
| 9 | + [string] $VsInstallOutputDir = "${env:System_DefaultWorkingDirectory}\vs", |
| 10 | + |
| 11 | + [System.IO.FileInfo] $VsInstallPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Enterprise", |
| 12 | + |
| 13 | + [System.IO.FileInfo] $VsInstallerPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer", |
| 14 | + |
| 15 | + [switch] $Collect = $false, |
| 16 | + |
| 17 | + [switch] $Cleanup = $false, |
| 18 | + |
| 19 | + [switch] $UseWebInstaller = $false |
| 20 | +) |
| 21 | + |
| 22 | +$Components | ForEach-Object { |
| 23 | + $componentList += '--add', $_ |
| 24 | +} |
| 25 | + |
| 26 | +$LocalVsInstaller = "$VsInstallerPath\vs_installershell.exe" |
| 27 | + |
| 28 | +$UseWebInstaller = $UseWebInstaller -or -not (Test-Path -Path "$LocalVsInstaller") |
| 29 | + |
| 30 | +if ($UseWebInstaller) { |
| 31 | + Write-Host "Downloading web installer..." |
| 32 | + |
| 33 | + Invoke-WebRequest -Method Get ` |
| 34 | + -Uri $InstallerUri ` |
| 35 | + -OutFile $VsInstaller |
| 36 | + |
| 37 | + New-Item -ItemType directory -Path $VsInstallOutputDir |
| 38 | + |
| 39 | + Write-Host "Running web installer to download requested components..." |
| 40 | + |
| 41 | + Start-Process ` |
| 42 | + -FilePath "$VsInstaller" ` |
| 43 | + -ArgumentList ( ` |
| 44 | + '--layout', "$VsInstallOutputDir", |
| 45 | + '--wait', |
| 46 | + '--norestart', |
| 47 | + '--quiet' + ` |
| 48 | + $componentList |
| 49 | + ) ` |
| 50 | + -Wait ` |
| 51 | + -PassThru |
| 52 | + |
| 53 | + Write-Host "Running downloaded VS installer to add requested components..." |
| 54 | + |
| 55 | + Start-Process ` |
| 56 | + -FilePath "$VsInstallOutputDir\vs_Enterprise.exe" ` |
| 57 | + -ArgumentList ( |
| 58 | + 'modify', |
| 59 | + '--installPath', "`"$VsInstallPath`"" , |
| 60 | + '--wait', |
| 61 | + '--norestart', |
| 62 | + '--quiet' + ` |
| 63 | + $componentList |
| 64 | + ) ` |
| 65 | + -Wait ` |
| 66 | + -PassThru ` |
| 67 | + -OutVariable returnCode |
| 68 | + |
| 69 | + if ($Cleanup) { |
| 70 | + Write-Host "Cleaning up..." |
| 71 | + |
| 72 | + Remove-Item -Path $VsInstaller |
| 73 | + Remove-Item -Path $VsInstallOutputDir -Recurse |
| 74 | + } |
| 75 | + |
| 76 | +} else { |
| 77 | + Write-Host "Running local installer to add requested components..." |
| 78 | + |
| 79 | + Start-Process ` |
| 80 | + -FilePath "$LocalVsInstaller" ` |
| 81 | + -ArgumentList ( |
| 82 | + 'modify', |
| 83 | + '--installPath', "`"$VsInstallPath`"" , |
| 84 | + '--norestart', |
| 85 | + '--quiet' + ` |
| 86 | + $componentList |
| 87 | + ) ` |
| 88 | + -Wait ` |
| 89 | + -OutVariable returnCode |
| 90 | +} |
| 91 | + |
| 92 | +if ($Collect) { |
| 93 | + Invoke-WebRequest -Method Get ` |
| 94 | + -Uri 'https://download.microsoft.com/download/8/3/4/834E83F6-C377-4DCE-A757-69A418B6C6DF/Collect.exe' ` |
| 95 | + -OutFile ${env:System_DefaultWorkingDirectory}\Collect.exe |
| 96 | + |
| 97 | + # Should generate ${env:Temp}\vslogs.zip |
| 98 | + Start-Process ` |
| 99 | + -FilePath "${env:System_DefaultWorkingDirectory}\Collect.exe" ` |
| 100 | + -Wait ` |
| 101 | + -PassThru |
| 102 | + |
| 103 | + New-Item -ItemType Directory -Force ${env:System_DefaultWorkingDirectory}\vslogs |
| 104 | + Expand-Archive -Path ${env:TEMP}\vslogs.zip -DestinationPath ${env:System_DefaultWorkingDirectory}\vslogs\ |
| 105 | + |
| 106 | + Write-Host "VC versions after installation:" |
| 107 | + Get-ChildItem -Name "$VsInstallPath\VC\Tools\MSVC\" |
| 108 | +} |
0 commit comments