11# ===============================
22# Windows Telemetry Blocker
3- # Script Version: nextgen-0.8 (pending review )
3+ # Script Version: nextgen-0.9 (pending)
44# ===============================
55
66
1313 [switch ]$WhatIf ,
1414 [switch ]$RollbackOnFailure ,
1515 [switch ]$Rollback ,
16- [switch ]$RestorePoint
16+ [switch ]$RestorePoint ,
17+ [switch ]$Update ,
18+ [switch ]$EnableAuditLog
1719)
1820
1921# --- Special parameter handling (outside param block) ---
@@ -81,6 +83,14 @@ Write-Host "===============================" -ForegroundColor Cyan
8183Write-Host (" Windows Telemetry Blocker v{0}" -f $ScriptVersion ) - ForegroundColor Cyan
8284Write-Host " ===============================" - ForegroundColor Cyan
8385
86+ # Check for updates if requested
87+ if ($Update ) {
88+ Write-Host " Update requested. Checking for updates..." - ForegroundColor Yellow
89+ Update-Script
90+ Write-Host " Update check complete." - ForegroundColor Green
91+ Write-AuditLog " Script update check performed"
92+ }
93+
8494# Fail fast for unhandled errors in scripts we call; we'll handle expected errors with try/catch
8595$ErrorActionPreference = ' Stop'
8696$VerbosePreference = ' Continue'
@@ -95,6 +105,7 @@ $errorLogFile = Join-Path $PSScriptRoot "telemetry-blocker-errors.log"
95105$executionStatsFile = Join-Path $PSScriptRoot " telemetry-blocker-stats.log"
96106$reportFile = Join-Path $PSScriptRoot " telemetry-blocker-report.md"
97107$modulesDir = Join-Path $PSScriptRoot " modules"
108+ $GitHubRepo = " https://github.com/N0tHorizon/WindowsTelemetryBlocker"
98109
99110# ==== Logging helpers (single authoritative definitions) ====
100111function Write-Log {
@@ -122,6 +133,66 @@ function Write-Stats {
122133 }
123134}
124135
136+ # ==== New Feature Functions ====
137+ function Update-Script {
138+ Write-Host " Fetching latest version from GitHub..." - ForegroundColor Yellow
139+ try {
140+ # Download the main script
141+ $mainUrl = " $GitHubRepo /raw/main/windowstelementryblocker.ps1"
142+ $tempMain = Join-Path $PSScriptRoot " temp_main.ps1"
143+ Invoke-WebRequest - Uri $mainUrl - OutFile $tempMain - ErrorAction Stop
144+
145+ # Download modules
146+ $apiUrl = " $GitHubRepo /contents/modules"
147+ $response = Invoke-WebRequest - Uri $apiUrl - Headers @ { " Accept" = " application/vnd.github.v3+json" } - ErrorAction Stop
148+ $files = $response.Content | ConvertFrom-Json
149+
150+ foreach ($file in $files ) {
151+ if ($file.name -like " *.ps1" ) {
152+ $fileUrl = $file.download_url
153+ $tempFile = Join-Path $PSScriptRoot (" temp_{0}" -f $file.name )
154+ Invoke-WebRequest - Uri $fileUrl - OutFile $tempFile - ErrorAction Stop
155+ }
156+ }
157+
158+ # Confirm
159+ $confirm = Read-Host " Downloaded updates. Overwrite files? (Y/N)"
160+ if ($confirm -eq ' Y' ) {
161+ # Overwrite
162+ Move-Item $tempMain (Join-Path $PSScriptRoot " windowstelementryblocker.ps1" ) - Force - ErrorAction Stop
163+ foreach ($file in $files ) {
164+ if ($file.name -like " *.ps1" ) {
165+ $tempFile = Join-Path $PSScriptRoot (" temp_{0}" -f $file.name )
166+ $dest = Join-Path $modulesDir $file.name
167+ Move-Item $tempFile $dest - Force - ErrorAction Stop
168+ }
169+ }
170+ Write-Host " Update complete." - ForegroundColor Green
171+ } else {
172+ Write-Host " Update cancelled." - ForegroundColor Yellow
173+ # Clean temp
174+ Remove-Item (Join-Path $PSScriptRoot " temp_*" ) - Force - ErrorAction SilentlyContinue
175+ }
176+ } catch {
177+ Write-Host (" [ERROR] Update failed: {0}" -f $_.Exception.Message ) - ForegroundColor Red
178+ # Clean temp
179+ Remove-Item (Join-Path $PSScriptRoot " temp_*" ) - Force - ErrorAction SilentlyContinue
180+ }
181+ }
182+
183+ function Write-AuditLog {
184+ param ([string ]$Message , [string ]$EventType = " Information" )
185+ if (-not $EnableAuditLog ) { return }
186+ try {
187+ if (-not (Get-EventLog - LogName Application - Source " TelemetryBlocker" - ErrorAction SilentlyContinue)) {
188+ New-EventLog - LogName Application - Source " TelemetryBlocker" - ErrorAction Stop
189+ }
190+ Write-EventLog - LogName Application - Source " TelemetryBlocker" - EventId 1000 - EntryType $EventType - Message $Message - ErrorAction Stop
191+ } catch {
192+ Write-Log (" Failed to write audit log: {0}" -f $_.Exception.Message ) - Error
193+ }
194+ }
195+
125196# ==== Registry backup/export before changes ====
126197function Export-RegistryBackup {
127198 try {
@@ -169,6 +240,7 @@ Write-Log "=== Script started ==="
169240Write-Log (" Windows Version: {0}" -f $winVersion )
170241Write-Log (" Windows Build: {0}" -f $winBuild )
171242Write-Log (" Script Version: {0}" -f $ScriptVersion )
243+ Write-AuditLog (" Script started - Version {0}, Windows {1} Build {2}" -f $ScriptVersion , $winVersion , $winBuild )
172244
173245# ==== Ensure modules directory exists ====
174246if (-not (Test-Path $modulesDir )) {
@@ -184,6 +256,14 @@ if (-not (Test-Path $modulesDir)) {
184256 }
185257}
186258
259+ # ==== Rollback coverage scan ====
260+ $rollbackCoverage = @ {}
261+ foreach ($mod in $moduleList ) {
262+ $rollbackPath = Join-Path $modulesDir (" {0}-rollback.ps1" -f $mod )
263+ $rollbackCoverage [$mod ] = Test-Path $rollbackPath
264+ }
265+ Write-Log (" Rollback coverage: {0}" -f (($rollbackCoverage.GetEnumerator () | ForEach-Object { " $ ( $_.Key ) :$ ( $_.Value ) " }) -join ' , ' ))
266+
187267# ==== System helpers ====
188268function New-SystemRestorePoint {
189269 try {
@@ -402,6 +482,8 @@ function Resolve-ModuleDependencies {
402482 return $resolved
403483}
404484
485+
486+
405487# ==== Module execution loop ====
406488Write-Host " `n === Starting Module Execution ===" - ForegroundColor Cyan
407489$summary = @ ()
@@ -414,6 +496,7 @@ $toRunResolved = Resolve-ModuleDependencies -mods $toRun
414496
415497foreach ($mod in $toRunResolved ) {
416498 Write-Host (" `n Running module: {0}" -f $mod ) - ForegroundColor Yellow
499+ Write-AuditLog (" Starting module execution: {0}" -f $mod )
417500 $moduleStart = Get-Date
418501 try {
419502 $modulePath = Join-Path $modulesDir (" {0}.ps1" -f $mod )
@@ -431,10 +514,12 @@ foreach ($mod in $toRunResolved) {
431514 if ($result -eq $false ) {
432515 Write-Host (" [ERROR] Module {0} reported failure" -f $mod ) - ForegroundColor Red
433516 Write-Log (" Module {0} reported failure" -f $mod ) - Error
517+ Write-AuditLog (" Module {0} execution failed" -f $mod ) " Warning"
434518 $summary += (" Module {0} reported failure" -f $mod )
435519 $moduleResults [$mod ] = @ { Status = ' Failure' ; Start = $moduleStart ; End = (Get-Date ) }
436520 } else {
437521 Write-Log (" Module {0} completed" -f $mod )
522+ Write-AuditLog (" Module {0} execution completed successfully" -f $mod )
438523 $summary += (" Module {0} completed" -f $mod )
439524 $moduleResults [$mod ] = @ { Status = ' Success' ; Start = $moduleStart ; End = (Get-Date ) }
440525 }
@@ -450,6 +535,7 @@ foreach ($mod in $toRunResolved) {
450535 if ($RollbackOnFailure ) {
451536 Write-Host " Rolling back changes for executed modules..." - ForegroundColor Red
452537 Write-Log (" Rollback initiated due to failure in module {0}" -f $mod )
538+ Write-AuditLog (" Rollback initiated due to failure in module {0}" -f $mod ) " Warning"
453539
454540 # clone and reverse executedModules safely
455541 $executedClone = @ ()
@@ -463,20 +549,24 @@ foreach ($mod in $toRunResolved) {
463549 Write-Host (" Running rollback for {0}..." -f $rmod ) - ForegroundColor Yellow
464550 . $rollbackPath
465551 Write-Log (" Rollback for {0} completed" -f $rmod )
552+ Write-AuditLog (" Rollback for {0} completed" -f $rmod )
466553 $rollbackModules += $rmod
467554 } catch {
468555 $rbErr = if ($_.Exception ) { $_.Exception.Message } else { $_.ToString () }
469556 Write-Host (" [ERROR] Rollback failed for {0}: {1}" -f $rmod , $rbErr ) - ForegroundColor Red
470557 Write-Log (" Rollback failed for {0}: {1}" -f $rmod , $rbErr ) - Error
558+ Write-AuditLog (" Rollback failed for {0}: {1}" -f $rmod , $rbErr ) " Error"
471559 }
472560 } else {
473561 Write-Host (" No rollback script for {0}" -f $rmod ) - ForegroundColor DarkYellow
474562 Write-Log (" No rollback script for {0}" -f $rmod )
563+ Write-AuditLog (" No rollback script for {0}" -f $rmod ) " Warning"
475564 }
476565 }
477566
478567 Write-Host " Rollback complete. Exiting." - ForegroundColor Red
479568 Write-Log " Rollback complete. Exiting."
569+ Write-AuditLog " Rollback complete. Exiting."
480570 break
481571 } else {
482572 $response = Read-Host " Do you want to continue with remaining modules? (Y/N)"
0 commit comments