Private/Invoke-WUAutoRepair.ps1
|
function Invoke-WUAutoRepair { <# .SYNOPSIS Automatically attempts to repair Windows Update issues based on error patterns. .DESCRIPTION Analyzes Windows Update errors and applies appropriate remediation strategies using existing module functions. This consolidates the auto-repair logic from Install-WindowsUpdate into a reusable private function. .PARAMETER Errors Array of error messages to analyze for repair strategy. .EXAMPLE Invoke-WUAutoRepair -Errors $result.Errors #> [CmdletBinding()] param( [Parameter(Mandatory)] [string[]]$Errors ) Write-Host "`n" + ("="*60) -ForegroundColor Yellow Write-Host " AUTOMATIC REPAIR INITIATED" -ForegroundColor Yellow Write-Host ("="*60) -ForegroundColor Yellow $errorString = $Errors -join " " $repairAttempted = $false $repairResults = @() try { if ($errorString -match "COMPONENT_STORE|0x80073712|0x800f0982|0x80070490") { Write-Host "Component store corruption detected - running component store repair..." -ForegroundColor Yellow try { $repairResult = Resolve-WUComponentStore -ForceSSU Write-Host "Component store repair completed with result: $($repairResult.OverallResult)" -ForegroundColor Green $repairResults += "Auto-repair attempted: Component store repair - $($repairResult.OverallResult)" $repairAttempted = $true } catch { Write-Host "Component store repair failed: $($_.Exception.Message)" -ForegroundColor Red $repairResults += "Auto-repair failed: Component store repair - $($_.Exception.Message)" } } elseif ($errorString -match "NOTDOWNLOADED|DOWNLOAD_FAILED|CONNECTION_ERROR|TIMEOUT") { Write-Host "Download/connectivity issues detected - running network troubleshooting..." -ForegroundColor Yellow try { $repairResult = Invoke-WUTroubleshooter Write-Host "Network troubleshooting completed" -ForegroundColor Green $repairResults += "Auto-repair attempted: Network troubleshooting completed" $repairAttempted = $true } catch { Write-Host "Network troubleshooting failed: $($_.Exception.Message)" -ForegroundColor Red $repairResults += "Auto-repair failed: Network troubleshooting - $($_.Exception.Message)" } } elseif ($errorString -match "INSTALL_NOT_ALLOWED|INSTALL_ALREADY_RUNNING") { Write-Host "Installation blocking detected - checking for pending operations..." -ForegroundColor Yellow try { $pendingUpdates = Get-WUPendingUpdates if ($pendingUpdates.PendingReboot) { Write-Host "Pending reboot detected - restart required before installation" -ForegroundColor Yellow $repairResults += "Auto-diagnosis: Pending reboot detected - restart required" } else { Write-Host "No pending reboot found - may be another installer running" -ForegroundColor Yellow $repairResults += "Auto-diagnosis: No pending reboot - check for other installers" } $repairAttempted = $true } catch { Write-Host "Pending operation check failed: $($_.Exception.Message)" -ForegroundColor Red $repairResults += "Auto-diagnosis failed: Pending operation check - $($_.Exception.Message)" } } if (-not $repairAttempted) { Write-Host "Generic Windows Update failure - running comprehensive repair..." -ForegroundColor Yellow try { $repairResult = Invoke-WUComprehensiveRemediation Write-Host "Comprehensive repair completed with result: $($repairResult.OverallResult)" -ForegroundColor Green $repairResults += "Auto-repair attempted: Comprehensive remediation - $($repairResult.OverallResult)" } catch { Write-Host "Comprehensive repair failed: $($_.Exception.Message)" -ForegroundColor Red $repairResults += "Auto-repair failed: Comprehensive remediation - $($_.Exception.Message)" } } } finally { Write-Host ("="*60) -ForegroundColor Yellow Write-Host "" } return $repairResults } |