1+ function Add-Vs {
2+ <#
3+ . SYNOPSIS
4+ Add the required Visual Studio components.
5+ . PARAMETER VsConfig
6+ Visual Studio Configuration
7+ . PARAMETER VsVersion
8+ Visual Studio Version
9+ #>
10+ [OutputType ()]
11+ param (
12+ [Parameter (Mandatory = $true , Position = 0 , HelpMessage = ' Visual Studio Version' )]
13+ [ValidateNotNull ()]
14+ [ValidateLength (1 , [int ]::MaxValue)]
15+ [string ] $VsVersion ,
16+ [Parameter (Mandatory = $true , Position = 1 , HelpMessage = ' Visual Studio Configuration' )]
17+ [PSCustomObject ] $VsConfig
18+ )
19+ begin {
20+ $vsWhereUrl = ' https://github.com/microsoft/vswhere/releases/latest/download/vswhere.exe'
21+ }
22+ process {
23+ $Config = $VsConfig.vs .$VsVersion
24+
25+ $installerDir = Join-Path " ${env: ProgramFiles(x86)} \Microsoft Visual Studio" ' Installer'
26+ $vswherePath = Join-Path $installerDir ' vswhere.exe'
27+ if (-not (Test-Path $vswherePath )) {
28+ if (-not (Test-Path $installerDir )) {
29+ New-Item - Path $installerDir - ItemType Directory - Force | Out-Null
30+ }
31+ Invoke-WebRequest - Uri $vsWhereUrl - OutFile $vswherePath - UseBasicParsing
32+ }
33+
34+ $instances = & $vswherePath - products ' *' - format json 2> $null | ConvertFrom-Json
35+ $vsInst = $instances | Select-Object - First 1
36+
37+ $componentArgs = $Config.components | ForEach-Object { ' --add' ; $_ }
38+
39+ if ($vsInst ) {
40+ [string ]$channel = $vsInst.installationVersion.Split (' .' )[0 ]
41+ if ($vsInst.catalog.productId -match ' (Enterprise|Professional|Community)$' ) {
42+ $exe = " vs_$ ( $Matches [1 ].ToLower()) .exe"
43+ } else {
44+ $exe = ' vs_buildtools.exe'
45+ }
46+
47+ $installerUrl = " https://aka.ms/vs/$channel /release/$exe "
48+ $installerPath = Join-Path $env: TEMP $exe
49+
50+ Invoke-WebRequest - Uri $installerUrl - OutFile $installerPath - UseBasicParsing
51+
52+ & $installerPath modify `
53+ -- installPath $vsInst.installationPath `
54+ -- quiet -- wait -- norestart -- nocache `
55+ @componentArgs 2>&1 | ForEach-Object { Write-Host $_ }
56+ } else {
57+ $channel = $VsVersion -replace ' \D' , ' '
58+ $exe = ' vs_buildtools.exe'
59+ $installerUrl = " https://aka.ms/vs/$channel /release/$exe "
60+ $installerPath = Join-Path $env: TEMP $exe
61+
62+ Invoke-WebRequest - Uri $installerUrl - OutFile $installerPath - UseBasicParsing
63+ & $installerPath `
64+ -- quiet -- wait -- norestart -- nocache `
65+ @componentArgs 2>&1 | ForEach-Object { Write-Host $_ }
66+ }
67+ }
68+ end {
69+ }
70+ }
0 commit comments