From fa2921025df5ba605a25b9469a1defa68e7f51bf Mon Sep 17 00:00:00 2001 From: ostro Date: Mon, 15 Jun 2026 17:12:44 +0100 Subject: [PATCH] Update LexmarkPrinterInstall.ps1 --- LexmarkPrinterInstall.ps1 | 110 ++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 63 deletions(-) diff --git a/LexmarkPrinterInstall.ps1 b/LexmarkPrinterInstall.ps1 index af709b4..0bf58d4 100644 --- a/LexmarkPrinterInstall.ps1 +++ b/LexmarkPrinterInstall.ps1 @@ -1,64 +1,48 @@ -# 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: $_" +# 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 +} +$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 +} else { + Write-Host "Driver INF found at $InfPath." -ForegroundColor Green +} +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)" +$IPAddress = $IPAddress.Trim() +$PrinterName = $PrinterName.Trim() +$PortName = "IP_$IPAddress" +Write-Host "`nStarting 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 +} +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 + } +} +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: $_" } \ No newline at end of file