Update LexmarkPrinterInstall.ps1
This commit is contained in:
+51
-12
@@ -2,30 +2,67 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
|
|||||||
Write-Warning "Please run this script as an Administrator!"
|
Write-Warning "Please run this script as an Administrator!"
|
||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
$DriverName = "Lexmark Universal v2 XL"
|
|
||||||
$InfPath = "C:\Windows\INF\oem59.inf"
|
# Define targets
|
||||||
Write-Host "Verifying driver availability..." -ForegroundColor Cyan
|
$TargetDriverName = "Lexmark Universal v2 XL"
|
||||||
if (-not (Test-Path -Path $InfPath)) {
|
$FallbackInfPath = "C:\Windows\INF\oem59.inf"
|
||||||
Write-Error "CRITICAL ERROR: The required driver file '$InfPath' was not found on this device."
|
$DriverName = $null
|
||||||
Write-Host "Installation aborted. Please stage the driver package before running this script." -ForegroundColor Red
|
$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
|
Exit
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Driver INF found at $InfPath." -ForegroundColor Green
|
$DriverName = $TargetDriverName
|
||||||
|
$NeedsRegistration = $true
|
||||||
|
Write-Host "Staging INF found at $FallbackInfPath." -ForegroundColor Green
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
$IPAddress = Read-Host -Prompt "Enter the printer's IP address (e.g., 192.168.1.50)"
|
$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)"
|
$PrinterName = Read-Host -Prompt "Enter the custom name for this printer (e.g., Service printer)"
|
||||||
$IPAddress = $IPAddress.Trim()
|
$IPAddress = $IPAddress.Trim()
|
||||||
$PrinterName = $PrinterName.Trim()
|
$PrinterName = $PrinterName.Trim()
|
||||||
$PortName = "IP_$IPAddress"
|
$PortName = "IP_$IPAddress"
|
||||||
|
|
||||||
Write-Host "Starting installation for '$PrinterName'..." -ForegroundColor Cyan
|
Write-Host "Starting installation for '$PrinterName'..." -ForegroundColor Cyan
|
||||||
Write-Host "Registering driver: $DriverName..." -ForegroundColor Yellow
|
|
||||||
|
# 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 {
|
try {
|
||||||
Add-PrinterDriver -Name $DriverName -InfPath $InfPath -ErrorAction Stop
|
Add-PrinterDriver -Name $DriverName -InfPath $FallbackInfPath -ErrorAction Stop
|
||||||
Write-Host "Driver successfully verified/registered." -ForegroundColor Green
|
Write-Host "Driver successfully registered." -ForegroundColor Green
|
||||||
} catch {
|
} catch {
|
||||||
Write-Host "Note: Driver registration status: $_" -ForegroundColor DarkYellow
|
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
|
Write-Host "Creating printer port: $PortName..." -ForegroundColor Yellow
|
||||||
if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) {
|
if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) {
|
||||||
Write-Host "Port $PortName already exists. Skipping port creation." -ForegroundColor Green
|
Write-Host "Port $PortName already exists. Skipping port creation." -ForegroundColor Green
|
||||||
@@ -38,10 +75,12 @@ if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) {
|
|||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Install Printer
|
||||||
Write-Host "Installing printer asset..." -ForegroundColor Yellow
|
Write-Host "Installing printer asset..." -ForegroundColor Yellow
|
||||||
try {
|
try {
|
||||||
Add-Printer -DriverName $DriverName -Name $PrinterName -PortName $PortName -ErrorAction Stop
|
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 {
|
} catch {
|
||||||
Write-Error "Failed to install the printer: $_"
|
Write-Error "Failed to install the printer: $_"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user