#requires -Version 5.1 [CmdletBinding()] param( [string]$Repository, [ValidatePattern('^[a-z0-9][a-z0-9-]*$')][string]$Machine, [ValidatePattern('^[A-Za-z0-9._/-]+$')][string]$Branch = 'main', [string]$Checkout = (Join-Path $env:LOCALAPPDATA 'ConfigCentral\repository'), [switch]$SkipApply, [switch]$RepairWinGetOnly ) Set-StrictMode -Version 2.0 $ErrorActionPreference = 'Stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $script:BootstrapScriptPath = if ($MyInvocation.MyCommand.PSObject.Properties['Path']) { [string]$MyInvocation.MyCommand.Path } else { $null } if ([string]::IsNullOrWhiteSpace($script:BootstrapScriptPath)) { throw 'bootstrap.ps1 must be downloaded and run as a file so administrator relaunch can work.' } function Test-Administrator { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($identity) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Invoke-BootstrapWingetInstall { param([Parameter(Mandatory = $true)][string]$Id) & winget.exe install --id $Id --exact --source winget --silent --disable-interactivity --accept-package-agreements --accept-source-agreements --scope machine if ($LASTEXITCODE -ne 0) { throw "WinGet failed to install '$Id' (exit $LASTEXITCODE)." } } function Find-PowerShell7Path { $command = Get-Command pwsh.exe -ErrorAction SilentlyContinue if ($command) { return $command.Source } if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) { $installedPath = Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' if (Test-Path -LiteralPath $installedPath -PathType Leaf) { return $installedPath } } return $null } function Find-GitPath { $command = Get-Command git.exe -ErrorAction SilentlyContinue if ($command) { return $command.Source } $candidates = @() if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) { $candidates += (Join-Path $env:ProgramFiles 'Git\cmd\git.exe') $candidates += (Join-Path $env:ProgramFiles 'Git\bin\git.exe') } $programFilesX86 = [Environment]::GetEnvironmentVariable('ProgramFiles(x86)') if (-not [string]::IsNullOrWhiteSpace($programFilesX86)) { $candidates += (Join-Path $programFilesX86 'Git\cmd\git.exe') } if (-not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { $candidates += (Join-Path $env:LOCALAPPDATA 'Programs\Git\cmd\git.exe') } foreach ($candidate in $candidates) { if (Test-Path -LiteralPath $candidate -PathType Leaf) { return $candidate } } return $null } function Update-BootstrapProcessPath { $pathValues = @( [Environment]::GetEnvironmentVariable('Path', 'Machine'), [Environment]::GetEnvironmentVariable('Path', 'User'), $env:Path ) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } $env:Path = $pathValues -join ';' } function Ensure-WinGet { if (Get-Command winget.exe -ErrorAction SilentlyContinue) { return } try { Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe -ErrorAction Stop } catch { Write-Verbose "Desktop App Installer registration did not restore WinGet: $($_.Exception.Message)" } if (Get-Command winget.exe -ErrorAction SilentlyContinue) { return } if (-not (Test-Administrator)) { $arguments = "-NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$script:BootstrapScriptPath`" -RepairWinGetOnly" $repair = Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList $arguments -Wait -PassThru if ($repair.ExitCode -ne 0) { throw "Elevated WinGet repair failed (exit $($repair.ExitCode))." } if (-not (Get-Command winget.exe -ErrorAction SilentlyContinue)) { $windowsApps = Join-Path $env:LOCALAPPDATA 'Microsoft\WindowsApps' if (($env:Path -split ';') -notcontains $windowsApps) { $env:Path = "$env:Path;$windowsApps" } } if (-not (Get-Command winget.exe -ErrorAction SilentlyContinue)) { throw 'WinGet was repaired but is unavailable in the current user session.' } return } if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) { Install-PackageProvider -Name NuGet -Force -Scope AllUsers | Out-Null } Install-Module -Name Microsoft.WinGet.Client -Repository PSGallery -Force -Scope AllUsers -AllowClobber Repair-WinGetPackageManager -AllUsers if (-not (Get-Command winget.exe -ErrorAction SilentlyContinue)) { throw 'WinGet repair completed but winget.exe remains unavailable.' } } if ($RepairWinGetOnly) { if (-not (Test-Administrator)) { throw 'WinGet repair requires administrator rights.' } if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) { Install-PackageProvider -Name NuGet -Force -Scope AllUsers | Out-Null } Install-Module -Name Microsoft.WinGet.Client -Repository PSGallery -Force -Scope AllUsers -AllowClobber Repair-WinGetPackageManager -AllUsers exit 0 } if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { throw 'Config Central bootstrap runs only on Windows.' } if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA) -or [string]::IsNullOrWhiteSpace($env:USERPROFILE)) { throw 'The interactive user profile is unavailable.' } if ([string]::IsNullOrWhiteSpace($Repository) -or [string]::IsNullOrWhiteSpace($Machine)) { throw 'Bootstrap requires -Repository and -Machine.' } Write-Host 'Checking WinGet...' Ensure-WinGet if (-not (Find-GitPath)) { Invoke-BootstrapWingetInstall -Id 'Git.Git' } if (-not (Find-PowerShell7Path)) { Invoke-BootstrapWingetInstall -Id 'Microsoft.PowerShell' } Update-BootstrapProcessPath $git = Find-GitPath if (-not $git) { throw 'Git was installed but git.exe could not be located.' } $powerShell7 = Find-PowerShell7Path if (-not $powerShell7) { throw 'PowerShell 7 was installed but pwsh.exe could not be located.' } if (Test-Path -LiteralPath (Join-Path $Checkout '.git')) { Push-Location $Checkout try { $dirty = & $git status --porcelain=v1 --untracked-files=all if ($LASTEXITCODE -ne 0) { throw 'Unable to inspect the existing checkout.' } if ($dirty) { throw "Existing checkout '$Checkout' has local changes; bootstrap will not replace them." } $origin = (& $git remote get-url origin).Trim() if ($origin -ne $Repository) { throw "Existing checkout origin '$origin' does not match '$Repository'." } & $git fetch --prune origin $Branch if ($LASTEXITCODE -ne 0) { throw 'Unable to fetch the configured branch.' } & $git checkout -B $Branch "origin/$Branch" if ($LASTEXITCODE -ne 0) { throw 'Unable to update the bootstrap checkout.' } } finally { Pop-Location } } else { $parent = Split-Path -Parent $Checkout New-Item -ItemType Directory -Path $parent -Force | Out-Null & $git clone --branch $Branch --single-branch $Repository $Checkout if ($LASTEXITCODE -ne 0) { throw "Unable to clone '$Repository'." } } $dataRoot = Join-Path $env:LOCALAPPDATA 'ConfigCentral' New-Item -ItemType Directory -Path $dataRoot -Force | Out-Null $settings = [ordered]@{ repositoryUrl = $Repository branch = $Branch machine = $Machine checkout = $Checkout userPaths = [ordered]@{ APPDATA = $env:APPDATA LOCALAPPDATA = $env:LOCALAPPDATA USERPROFILE = $env:USERPROFILE PROGRAMDATA = $env:PROGRAMDATA SYSTEMROOT = $env:SYSTEMROOT } } $settings | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $dataRoot 'settings.json') -Encoding UTF8 $launcherDirectory = Join-Path $env:LOCALAPPDATA 'ConfigCentral\bin' New-Item -ItemType Directory -Path $launcherDirectory -Force | Out-Null $launcherPath = Join-Path $launcherDirectory 'cc.cmd' $launcherContent = "@echo off`r`npwsh.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$Checkout\cc.ps1`" %*`r`n" [System.IO.File]::WriteAllText($launcherPath, $launcherContent, [Text.Encoding]::ASCII) $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (($userPath -split ';') -notcontains $launcherDirectory) { [Environment]::SetEnvironmentVariable('Path', (($userPath.TrimEnd(';') + ';' + $launcherDirectory).TrimStart(';')), 'User') $env:Path = "$env:Path;$launcherDirectory" } Write-Host "Config Central configured for '$Machine'." if (-not $SkipApply) { & $powerShell7 -NoLogo -NoProfile -ExecutionPolicy Bypass -File (Join-Path $Checkout 'cc.ps1') apply -Machine $Machine -DataRoot $dataRoot -RepositoryRoot $Checkout exit $LASTEXITCODE }