if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Warning "Please run this script as an Administrator!" 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 { # 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" Write-Host "Starting installation for '$PrinterName'..." -ForegroundColor Cyan # 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 } else { try { Add-PrinterPort -Name $PortName -PrinterHostAddress $IPAddress -ErrorAction Stop Write-Host "Port successfully created." -ForegroundColor Green } catch { Write-Error "Failed to create printer port: $_" 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 using driver '$DriverName'!" -ForegroundColor Green } catch { Write-Error "Failed to install the printer: $_" }