Private/Remove-WUProblematicDrivers.ps1
function Remove-WUProblematicDrivers { <# .SYNOPSIS Identifies and logs problematic drivers that may interfere with Windows Updates. .DESCRIPTION Scans for devices with configuration errors that could block Windows Update operations. This function identifies but does not automatically remove drivers to prevent system instability. .PARAMETER LogPath Path to the log file for detailed logging. .EXAMPLE $result = Remove-WUProblematicDrivers -LogPath "C:\Logs\wu.log" .NOTES This is a private function used internally by the WindowsUpdateTools module. Returns an object with Success, DriversRemoved, and ActionsPerformed properties. #> [CmdletBinding()] param( [string]$LogPath ) $result = [PSCustomObject]@{ Success = $false DriversRemoved = 0 ActionsPerformed = @() } try { Write-WULog -Message "Checking for problematic drivers..." -LogPath $LogPath # Find devices with configuration issues $problematicDevices = Get-CimInstance -ClassName Win32_PnPEntity | Where-Object { $_.ConfigManagerErrorCode -ne 0 -and $null -ne $_.ConfigManagerErrorCode } if ($problematicDevices) { Write-WULog -Message "Found $($problematicDevices.Count) devices with configuration errors" -LogPath $LogPath foreach ($device in $problematicDevices) { Write-WULog -Message "Problematic device: $($device.Name) (Error: $($device.ConfigManagerErrorCode))" -LogPath $LogPath } $result.ActionsPerformed += "Identified problematic devices" } else { Write-WULog -Message "No problematic drivers detected" -LogPath $LogPath } $result.Success = $true } catch { Write-WULog -Message "Error during driver cleanup: $($_.Exception.Message)" -Level Warning -LogPath $LogPath } return $result } |