diff --git a/samy.ps1 b/samy.ps1 index 4396c3a..ad26450 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -2200,28 +2200,135 @@ function Ensure-SamyPrinterDriver { Write-LogHybrid "Printer driver '$driverName' not found. Preparing to install." Info Printers -LogToEvent - # 1) Start with any static local path, if defined + # ----------------------------- + # 0) Decide where driver files live locally + # ----------------------------- + $localDriverRoot = $null + if ($Profile.PSObject.Properties.Name -contains 'DriverInfPath' -and $Profile.DriverInfPath) { + $localDriverRoot = Split-Path -Path $Profile.DriverInfPath -Parent + } + else { + # Fallback root if no INF path is defined yet + $localDriverRoot = "C:\ProgramData\SVS\Samy\Drivers" + } + + if (-not (Test-Path $localDriverRoot)) { + New-Item -Path $localDriverRoot -ItemType Directory -Force | Out-Null + } + + # ----------------------------- + # 1) Start with any static local INF path, if defined + # ----------------------------- $infPath = $null if ($Profile.PSObject.Properties.Name -contains 'DriverInfPath' -and $Profile.DriverInfPath) { if (Test-Path $Profile.DriverInfPath) { $infPath = $Profile.DriverInfPath + Write-LogHybrid "Using existing INF path '$infPath' for driver '$driverName'." Info Printers -LogToEvent } else { Write-LogHybrid "Configured DriverInfPath '$($Profile.DriverInfPath)' does not exist, will try repo download." Warning Printers -LogToEvent } } - # 2) If no usable INF path, fall back to downloading from repo - if (-not $infPath) { - if ($Profile.PSObject.Properties.Name -contains 'DriverPackagePath' -or - $Profile.PSObject.Properties.Name -contains 'DriverPackageUrl') { + # ----------------------------- + # 2) Try downloading a driver package from repo (404 is *not* fatal) + # ----------------------------- + $packageDownloaded = $false - $infPath = Get-SamyDriverInfFromRepo -Profile $Profile + if ($Profile.PSObject.Properties.Name -contains 'DriverPackagePath' -and $Profile.DriverPackagePath) { + $driverPackageUrl = "$Script:SamyRepoBase/$Script:SamyBranch/$($Profile.DriverPackagePath)?raw=1" + $localZip = Join-Path $localDriverRoot "package.zip" + + Write-LogHybrid "Attempting to download driver package from $driverPackageUrl." Info Printers -LogToEvent + + try { + Invoke-WebRequest -Uri $driverPackageUrl -OutFile $localZip -UseBasicParsing -ErrorAction Stop + Write-LogHybrid "Downloaded driver package from $driverPackageUrl to $localZip." Success Printers -LogToEvent + $packageDownloaded = $true + } + catch [System.Net.WebException] { + $response = $_.Exception.Response + $statusCode = $null + if ($response -and $response.StatusCode) { + $statusCode = [int]$response.StatusCode + } + + if ($statusCode -eq 404) { + # ★ This is the new behavior: warn, but do NOT throw. + Write-LogHybrid "Driver package not found at $driverPackageUrl (404). Falling back to INF-only install for '$($Profile.DisplayName)'." Warning Printers -LogToEvent + # We just continue – maybe a local INF exists or will exist. + } + else { + Write-LogHybrid "Driver package download failed ($statusCode) from $driverPackageUrl: $($_.Exception.Message)" Error Printers -LogToEvent + throw "Failed to download driver package from $driverPackageUrl: $($_.Exception.Message)" + } + } + catch { + Write-LogHybrid "Driver package download failed from $driverPackageUrl: $($_.Exception.Message)" Error Printers -LogToEvent + throw "Failed to download driver package from $driverPackageUrl: $($_.Exception.Message)" + } + } + else { + Write-LogHybrid "No DriverPackagePath defined for '$($Profile.DisplayName)'; will rely on local INF." Info Printers -LogToEvent + } + + # ----------------------------- + # 2b) If we *did* download a package, expand it and try to locate the INF + # ----------------------------- + if ($packageDownloaded) { + try { + Expand-Archive -Path $localZip -DestinationPath $localDriverRoot -Force + Write-LogHybrid "Expanded driver package to '$localDriverRoot'." Info Printers -LogToEvent + } + catch { + Write-LogHybrid "Failed to expand driver package '$localZip': $($_.Exception.Message)" Error Printers -LogToEvent + throw "Failed to expand driver package '$localZip': $($_.Exception.Message)" + } + + # If we don't yet have an INF path, try to derive it from DriverInfName + if (-not $infPath) { + if ($Profile.PSObject.Properties.Name -contains 'DriverInfName' -and $Profile.DriverInfName) { + $candidateInf = Join-Path $localDriverRoot $Profile.DriverInfName + if (Test-Path $candidateInf) { + $infPath = $candidateInf + Write-LogHybrid "Resolved INF from package as '$infPath' using DriverInfName '$($Profile.DriverInfName)'." Info Printers -LogToEvent + } + else { + Write-LogHybrid "Expected INF '$candidateInf' (from DriverInfName) not found after expanding package." Warning Printers -LogToEvent + } + } + else { + Write-LogHybrid "DriverInfName not defined for profile '$($Profile.ProfileName)'; cannot auto-resolve INF from expanded package." Warning Printers -LogToEvent + } } } - # 3) Still nothing? Hard fail with a clear message + # ----------------------------- + # 3) Legacy fallback: if you still want to use Get-SamyDriverInfFromRepo when we have no INF + # (optional – keep or remove depending on whether you still use that helper) + # ----------------------------- + if (-not $infPath) { + if ($Profile.PSObject.Properties.Name -contains 'DriverPackageUrl' -or + $Profile.PSObject.Properties.Name -contains 'DriverPackagePath') { + + # If you still have this helper and like its behavior, keep this: + try { + $infPath = Get-SamyDriverInfFromRepo -Profile $Profile + } + catch { + # If that helper throws on 404, you'll still see an error, so you might choose + # to *remove* this block once you're happy with the new archive behavior. + Write-LogHybrid "Get-SamyDriverInfFromRepo failed: $($_.Exception.Message)" Error Printers -LogToEvent + # Re-throw or not depending on how strict you want to be: + throw + } + } + } + + # ----------------------------- + # 4) Still nothing? Hard fail with a clear message + # ----------------------------- if (-not $infPath -or -not (Test-Path $infPath)) { - throw "Driver '$driverName' is not installed and no valid DriverInfPath or DriverPackagePath/DriverPackageUrl is defined for profile '$($Profile.ProfileName)'." + 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 @@ -2239,6 +2346,7 @@ function Ensure-SamyPrinterDriver { } + function Install-SamyTcpIpPrinter { [CmdletBinding()] param(