# Ensure the script is running as Administrator 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 driver variables at the top for verification $DriverName = "Lexmark Universal v2 XL" $InfPath = "C:\Windows\INF\oem59.inf" # 1. Pre-check: Terminate if the driver INF is missing from the device 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 } else { Write-Host "Driver INF found at $InfPath." -ForegroundColor Green } # 2. Prompt the user for the IP Address and Printer Name Write-Host "" $IPAddress = Read-Host -Prompt "Enter the printer's IP address (e.g., 10.59.121.22)" $PrinterName = Read-Host -Prompt "Enter the custom name for this printer (e.g., Skoda Sales)" # Clean up any accidental leading/trailing spaces $IPAddress = $IPAddress.Trim() $PrinterName = $PrinterName.Trim() $PortName = "IP_$IPAddress" Write-Host "`nStarting installation for '$PrinterName'..." -ForegroundColor Cyan # 3. Register the Printer Driver into the Spooler 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 { # If it's already registered, Add-PrinterDriver might throw a non-critical exception Write-Host "Note: Driver registration status: $_" -ForegroundColor DarkYellow } # 4. Create the Printer Port (Checks if it exists first to avoid duplicates) 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 } } # 5. Install the 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 } catch { Write-Error "Failed to install the printer: $_" }