diff --git a/samy.ps1 b/samy.ps1 index be3df41..7037796 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -2257,16 +2257,47 @@ function Ensure-SamyPrinterDriver { throw "Driver '$driverName' is not installed and no valid DriverInfPath or usable driver package is available for profile '$($Profile.ProfileName)'." } - Write-LogHybrid "Installing printer driver '$driverName' from '$infPath'." Info Printers -LogToEvent - - Write-LogHybrid "Installing printer driver '$driverName' from '$infPath'." Info Printers -LogToEvent + Write-LogHybrid "Installing printer driver '$driverName' from '$infPath'." Info Printers -LogToEvent # Run pnputil and capture output + exit code + $pnputilCmd = "pnputil.exe /add-driver `"$infPath`" /install" + Write-LogHybrid "Running: $pnputilCmd" Info Printers -LogToEvent + $pnputilOutput = & pnputil.exe /add-driver "`"$infPath`"" /install 2>&1 $exitCode = $LASTEXITCODE Write-LogHybrid "pnputil exit code: $exitCode. Output:`n$pnputilOutput" Info Printers -LogToEvent + if ($exitCode -ne 0) { + throw "pnputil failed with exit code $exitCode installing '$driverName' from '$infPath'." + } + + # Best-effort verification: try to see if a matching SHARP driver is visible now, + # but DO NOT hard-fail if we can't find it by exact name. + $existingDriver = Get-PrinterDriver -ErrorAction SilentlyContinue | Where-Object { + $_.Name -eq $driverName -or + $_.Name -like "*$driverName*" -or + $driverName -like "*$($_.Name)*" + } + + if (-not $existingDriver) { + $sharpDrivers = Get-PrinterDriver -ErrorAction SilentlyContinue | + Where-Object { $_.Name -like "SHARP*" } + + $sharpList = if ($sharpDrivers) { + ($sharpDrivers | Select-Object -ExpandProperty Name) -join ', ' + } else { + '(none)' + } + + Write-LogHybrid "After pnputil, driver '$driverName' not found. Existing SHARP drivers: $sharpList" Warning Printers -LogToEvent + # NOTE: we intentionally do NOT throw here anymore. + } + else { + Write-LogHybrid "Printer driver '$($existingDriver.Name)' is present after pnputil (requested '$driverName')." Success Printers -LogToEvent + } + + if ($exitCode -ne 0) { throw "pnputil failed with exit code $exitCode. See Printers logs for details." }