diff --git a/LexmarkPrinterInstall.ps1 b/LexmarkPrinterInstall.ps1 index 0391ae6..e66fc85 100644 --- a/LexmarkPrinterInstall.ps1 +++ b/LexmarkPrinterInstall.ps1 @@ -2,30 +2,67 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti Write-Warning "Please run this script as an Administrator!" Exit } -$DriverName = "Lexmark Universal v2 XL" -$InfPath = "C:\Windows\INF\oem59.inf" -Write-Host "Verifying driver availability..." -ForegroundColor Cyan -if (-not (Test-Path -Path $InfPath)) { - Write-Error "CRITICAL ERROR: The required driver file '$InfPath' was not found on this device." - Write-Host "Installation aborted. Please stage the driver package before running this script." -ForegroundColor Red - Exit + +# Define targets +$TargetDriverName = "Lexmark Universal v2 XL" +$FallbackInfPath = "C:\Windows\INF\oem59.inf" +$DriverName = $null +$NeedsRegistration = $false + +Write-Host "Checking for installed Lexmark drivers..." -ForegroundColor Cyan + +# 1. Look for the exact desired driver first +$InstalledDrivers = Get-PrinterDriver -ErrorAction SilentlyContinue +$ExactDriver = $InstalledDrivers | Where-Object { $_.Name -eq $TargetDriverName } + +if ($ExactDriver) { + $DriverName = $ExactDriver.Name + Write-Host "Found exact driver match: '$DriverName'" -ForegroundColor Green } else { - Write-Host "Driver INF found at $InfPath." -ForegroundColor Green + # 2. Look for any other installed Lexmark driver as a fallback + $AnyLexmark = $InstalledDrivers | Where-Object { $_.Name -like "*Lexmark*" } | Select-Object -First 1 + if ($AnyLexmark) { + $DriverName = $AnyLexmark.Name + Write-Host "Exact match not found. Using installed fallback: '$DriverName'" -ForegroundColor Yellow + } else { + # 3. No driver found; attempt to stage from INF + Write-Host "No Lexmark drivers found installed. Attempting to stage from INF..." -ForegroundColor Yellow + if (-not (Test-Path -Path $FallbackInfPath)) { + Write-Error "CRITICAL ERROR: No Lexmark driver is installed, and the staging file '$FallbackInfPath' was not found." + Write-Host "Installation aborted. Please install a Lexmark driver or stage the INF package." -ForegroundColor Red + Exit + } else { + $DriverName = $TargetDriverName + $NeedsRegistration = $true + Write-Host "Staging INF found at $FallbackInfPath." -ForegroundColor Green + } + } } + Write-Host "" $IPAddress = Read-Host -Prompt "Enter the printer's IP address (e.g., 192.168.1.50)" $PrinterName = Read-Host -Prompt "Enter the custom name for this printer (e.g., Service printer)" $IPAddress = $IPAddress.Trim() $PrinterName = $PrinterName.Trim() -$PortName = "IP_$IPAddress" +$PortName = "IP_$IPAddress" + Write-Host "Starting installation for '$PrinterName'..." -ForegroundColor Cyan -Write-Host "Registering driver: $DriverName..." -ForegroundColor Yellow -try { - Add-PrinterDriver -Name $DriverName -InfPath $InfPath -ErrorAction Stop - Write-Host "Driver successfully verified/registered." -ForegroundColor Green -} catch { - Write-Host "Note: Driver registration status: $_" -ForegroundColor DarkYellow + +# Register driver only if we detected it was missing and the INF exists +if ($NeedsRegistration) { + Write-Host "Registering driver from INF: $DriverName..." -ForegroundColor Yellow + try { + Add-PrinterDriver -Name $DriverName -InfPath $FallbackInfPath -ErrorAction Stop + Write-Host "Driver successfully registered." -ForegroundColor Green + } catch { + Write-Error "Failed to register driver: $_" + Exit + } +} else { + Write-Host "Using pre-installed driver: $DriverName" -ForegroundColor Green } + +# Create Printer Port Write-Host "Creating printer port: $PortName..." -ForegroundColor Yellow if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) { Write-Host "Port $PortName already exists. Skipping port creation." -ForegroundColor Green @@ -38,10 +75,12 @@ if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) { Exit } } + +# Install Printer Write-Host "Installing printer asset..." -ForegroundColor Yellow try { Add-Printer -DriverName $DriverName -Name $PrinterName -PortName $PortName -ErrorAction Stop - Write-Host "Successfully installed '$PrinterName' on port $PortName!" -ForegroundColor Green + Write-Host "Successfully installed '$PrinterName' on port $PortName using driver '$DriverName'!" -ForegroundColor Green } catch { Write-Error "Failed to install the printer: $_" } \ No newline at end of file