[CmdletBinding()] $Phase = 'either' $ErrorActionPreference = 'Stop' $ToolRoot = Join-Path $env:ProgramData 'Sweep' $LogRoot = Join-Path $ToolRoot 'Logs' New-Item -ItemType Directory -Force -Path $LogRoot | Out-Null $LogFile = Join-Path $LogRoot ("run-{0:yyyyMMdd-HHmmss}.log" -f (Get-Date)) function Write-RunLog([string]$Message) { $line = "$(Get-Date -Format o) $Message"; $line | Tee-Object -FilePath $LogFile -Append } $Actions = @( ) $Requested = @($Actions | Where-Object { $Phase -eq 'either' -or $_.Phase -eq 'either' -or $_.Phase -eq $Phase }) if ($Requested.Count -eq 0) { Write-Host 'No selected tweaks apply to this phase.'; exit 0 } if ($false) { throw 'Administrator rights are required for one or more selected tweaks.' } $build = [Environment]::OSVersion.Version.Build if ($build -notin 22631, 26100, 26200) { throw "Unsupported Windows build: $build. No changes were made." } Write-RunLog "Windows build $build; phase $Phase; selected: $($Requested.Name -join ', ')" try { Checkpoint-Computer -Description 'Before Sweep changes' -RestorePointType 'MODIFY_SETTINGS' -ErrorAction SilentlyContinue } catch { Write-RunLog "Restore point unavailable: $($_.Exception.Message)" } $total = $Requested.Count $completed = 0 foreach ($action in $Requested) { $current = $completed + 1 $percent = [math]::Floor(($completed / $total) * 100) Write-Progress -Activity 'Applying Windows configuration' -Status "[$current/$total] $($action.Name)" -PercentComplete $percent Write-RunLog "[$current/$total] $($action.Name): started" try { & $action.Run; Write-RunLog "[$current/$total] $($action.Name): success" } catch { Write-RunLog "[$current/$total] $($action.Name): failed - $($_.Exception.Message)" } $completed++ } Write-Progress -Activity 'Applying Windows configuration' -Completed @{ toolVersion = '1.0.0'; schemaVersion = 1; phase = $Phase; build = $build; actions = @($Requested.Name); log = $LogFile } | ConvertTo-Json | Set-Content (Join-Path $ToolRoot 'applied-config.json') Write-RunLog 'Completed. Sign out or restart if Explorer changes are not visible.'