From 2b72e4b3adc9a5d93bec31daadf5b3f14c977dd7 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Fri, 5 Dec 2025 17:09:10 -0500 Subject: [PATCH] Update samy.ps1 --- samy.ps1 | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 5 deletions(-) diff --git a/samy.ps1 b/samy.ps1 index 554a08c..59e3443 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -1740,6 +1740,134 @@ function Invoke-CleanupSVSMSP { #region Printer handlers +function Get-SamyDriverRootFolder { + [CmdletBinding()] + param() + + $root = Join-Path $env:ProgramData 'SVS\Samy\Drivers' + + if (-not (Test-Path $root)) { + try { + New-Item -Path $root -ItemType Directory -Force | Out-Null + Write-LogHybrid "Created driver root folder '$root'." Info Printers -LogToEvent + } catch { + Write-LogHybrid "Failed to create driver root folder '$root': $($_.Exception.Message)" Warning Printers -LogToEvent + } + } + + return $root +} + +function Get-SamyDriverFolderForProfile { + [CmdletBinding()] + param( + [Parameter(Mandatory)][pscustomobject]$Profile + ) + + $root = Get-SamyDriverRootFolder + + # Optional override if you ever add DriverFolderName to the profile + if ($Profile.PSObject.Properties.Name -contains 'DriverFolderName' -and $Profile.DriverFolderName) { + $folderName = $Profile.DriverFolderName + } else { + $folderName = "$($Profile.ClientCode)_$($Profile.ProfileName)" + } + + $dest = Join-Path $root $folderName + + if (-not (Test-Path $dest)) { + try { + New-Item -Path $dest -ItemType Directory -Force | Out-Null + Write-LogHybrid "Created driver folder '$dest'." Info Printers -LogToEvent + } catch { + Write-LogHybrid "Failed to create driver folder '$dest': $($_.Exception.Message)" Warning Printers -LogToEvent + } + } + + return $dest +} + +function Get-SamyDriverPackageUrl { + [CmdletBinding()] + param( + [Parameter(Mandatory)][pscustomobject]$Profile + ) + + # If profile explicitly provides a full URL, prefer that + if ($Profile.PSObject.Properties.Name -contains 'DriverPackageUrl' -and $Profile.DriverPackageUrl) { + return $Profile.DriverPackageUrl + } + + # Otherwise build it from SamyRepoBase / SamyBranch and DriverPackagePath + if ($Profile.PSObject.Properties.Name -contains 'DriverPackagePath' -and $Profile.DriverPackagePath) { + # Example: https://git.../SAMY/raw/branch/beta/Drivers/.../package.zip?raw=1 + return "$Script:SamyRepoBase/$Script:SamyBranch/$($Profile.DriverPackagePath)?raw=1" + } + + return $null +} + +function Get-SamyDriverInfFromRepo { + [CmdletBinding()] + param( + [Parameter(Mandatory)][pscustomobject]$Profile + ) + + $url = Get-SamyDriverPackageUrl -Profile $Profile + $targetDir = Get-SamyDriverFolderForProfile -Profile $Profile + + if (-not $url) { + throw "No DriverPackageUrl or DriverPackagePath defined for profile '$($Profile.ProfileName)'." + } + + $zipPath = Join-Path $targetDir 'driver.zip' + + Write-LogHybrid "Downloading driver package for profile '$($Profile.ProfileName)' from $url" Info Printers -LogToEvent + + try { + Invoke-WebRequest -Uri $url -OutFile $zipPath -UseBasicParsing -ErrorAction Stop + } catch { + throw "Failed to download driver package from $url: $($_.Exception.Message)" + } + + # Extract zip + try { + Expand-Archive -Path $zipPath -DestinationPath $targetDir -Force + } catch { + throw "Failed to extract driver package '$zipPath': $($_.Exception.Message)" + } finally { + Remove-Item -Path $zipPath -ErrorAction SilentlyContinue + } + + # Decide which INF to use + $infPath = $null + + if ($Profile.PSObject.Properties.Name -contains 'DriverInfName' -and $Profile.DriverInfName) { + $candidate = Join-Path $targetDir $Profile.DriverInfName + if (Test-Path $candidate) { + $infPath = $candidate + } else { + Write-LogHybrid "DriverInfName '$($Profile.DriverInfName)' not found under '$targetDir'." Warning Printers -LogToEvent + } + } + + if (-not $infPath) { + $inf = Get-ChildItem -Path $targetDir -Recurse -Filter '*.inf' -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($inf) { + $infPath = $inf.FullName + } + } + + if (-not $infPath -or -not (Test-Path $infPath)) { + throw "No INF file found in extracted driver package for profile '$($Profile.ProfileName)'." + } + + Write-LogHybrid "Using driver INF '$infPath' for profile '$($Profile.ProfileName)'." Info Printers -LogToEvent + return $infPath +} + + + function Get-SamyClientListFromServer { <# .SYNOPSIS @@ -2063,27 +2191,54 @@ function Ensure-SamyPrinterDriver { throw "Profile '$($Profile.ProfileName)' has no DriverName defined in printer config." } + # Already installed? $existingDriver = Get-PrinterDriver -Name $driverName -ErrorAction SilentlyContinue if ($existingDriver) { - Write-Verbose "Printer driver '$driverName' already installed." + Write-LogHybrid "Printer driver '$driverName' already installed." Info Printers -LogToEvent return } - if (-not $Profile.DriverInfPath) { - throw "Driver '$driverName' is not installed and no DriverInfPath is defined for profile '$($Profile.ProfileName)'." + Write-LogHybrid "Printer driver '$driverName' not found. Preparing to install." Info Printers -LogToEvent + + # 1) Start with any static local path, if defined + $infPath = $null + if ($Profile.PSObject.Properties.Name -contains 'DriverInfPath' -and $Profile.DriverInfPath) { + if (Test-Path $Profile.DriverInfPath) { + $infPath = $Profile.DriverInfPath + } else { + Write-LogHybrid "Configured DriverInfPath '$($Profile.DriverInfPath)' does not exist, will try repo download." Warning Printers -LogToEvent + } } - $infPath = $Profile.DriverInfPath - Write-Verbose "Installing printer driver '$driverName' from '$infPath'." + # 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') { + $infPath = Get-SamyDriverInfFromRepo -Profile $Profile + } + } + + # 3) 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)'." + } + + Write-LogHybrid "Installing printer driver '$driverName' from '$infPath'." Info Printers -LogToEvent + + # Install with pnputil pnputil.exe /add-driver "`"$infPath`"" /install | Out-Null + # Verify $existingDriver = Get-PrinterDriver -Name $driverName -ErrorAction SilentlyContinue if (-not $existingDriver) { throw "Failed to install printer driver '$driverName' from '$infPath'." } + + Write-LogHybrid "Printer driver '$driverName' installed successfully." Success Printers -LogToEvent } + function Install-SamyTcpIpPrinter { [CmdletBinding()] param(