Update LexmarkPrinterInstall.ps1
This commit is contained in:
+47
-63
@@ -1,64 +1,48 @@
|
|||||||
# Ensure the script is running as Administrator
|
# Ensure the script is running as Administrator
|
||||||
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||||
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"
|
||||||
# Define driver variables at the top for verification
|
$InfPath = "C:\Windows\INF\oem59.inf"
|
||||||
$DriverName = "Lexmark Universal v2 XL"
|
Write-Host "Verifying driver availability..." -ForegroundColor Cyan
|
||||||
$InfPath = "C:\Windows\INF\oem59.inf"
|
if (-not (Test-Path -Path $InfPath)) {
|
||||||
|
Write-Error "CRITICAL ERROR: The required driver file '$InfPath' was not found on this device."
|
||||||
# 1. Pre-check: Terminate if the driver INF is missing from the device
|
Write-Host "Installation aborted. Please stage the driver package before running this script." -ForegroundColor Red
|
||||||
Write-Host "Verifying driver availability..." -ForegroundColor Cyan
|
Exit
|
||||||
if (-not (Test-Path -Path $InfPath)) {
|
} else {
|
||||||
Write-Error "CRITICAL ERROR: The required driver file '$InfPath' was not found on this device."
|
Write-Host "Driver INF found at $InfPath." -ForegroundColor Green
|
||||||
Write-Host "Installation aborted. Please stage the driver package before running this script." -ForegroundColor Red
|
}
|
||||||
Exit
|
Write-Host ""
|
||||||
} else {
|
$IPAddress = Read-Host -Prompt "Enter the printer's IP address (e.g., 10.59.121.22)"
|
||||||
Write-Host "Driver INF found at $InfPath." -ForegroundColor Green
|
$PrinterName = Read-Host -Prompt "Enter the custom name for this printer (e.g., Skoda Sales)"
|
||||||
}
|
$IPAddress = $IPAddress.Trim()
|
||||||
|
$PrinterName = $PrinterName.Trim()
|
||||||
# 2. Prompt the user for the IP Address and Printer Name
|
$PortName = "IP_$IPAddress"
|
||||||
Write-Host ""
|
Write-Host "`nStarting installation for '$PrinterName'..." -ForegroundColor Cyan
|
||||||
$IPAddress = Read-Host -Prompt "Enter the printer's IP address (e.g., 10.59.121.22)"
|
Write-Host "Registering driver: $DriverName..." -ForegroundColor Yellow
|
||||||
$PrinterName = Read-Host -Prompt "Enter the custom name for this printer (e.g., Skoda Sales)"
|
try {
|
||||||
|
Add-PrinterDriver -Name $DriverName -InfPath $InfPath -ErrorAction Stop
|
||||||
# Clean up any accidental leading/trailing spaces
|
Write-Host "Driver successfully verified/registered." -ForegroundColor Green
|
||||||
$IPAddress = $IPAddress.Trim()
|
} catch {
|
||||||
$PrinterName = $PrinterName.Trim()
|
Write-Host "Note: Driver registration status: $_" -ForegroundColor DarkYellow
|
||||||
$PortName = "IP_$IPAddress"
|
}
|
||||||
|
Write-Host "Creating printer port: $PortName..." -ForegroundColor Yellow
|
||||||
Write-Host "`nStarting installation for '$PrinterName'..." -ForegroundColor Cyan
|
if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) {
|
||||||
|
Write-Host "Port $PortName already exists. Skipping port creation." -ForegroundColor Green
|
||||||
# 3. Register the Printer Driver into the Spooler
|
} else {
|
||||||
Write-Host "Registering driver: $DriverName..." -ForegroundColor Yellow
|
try {
|
||||||
try {
|
Add-PrinterPort -Name $PortName -PrinterHostAddress $IPAddress -ErrorAction Stop
|
||||||
Add-PrinterDriver -Name $DriverName -InfPath $InfPath -ErrorAction Stop
|
Write-Host "Port successfully created." -ForegroundColor Green
|
||||||
Write-Host "Driver successfully verified/registered." -ForegroundColor Green
|
} catch {
|
||||||
} catch {
|
Write-Error "Failed to create printer port: $_"
|
||||||
# If it's already registered, Add-PrinterDriver might throw a non-critical exception
|
Exit
|
||||||
Write-Host "Note: Driver registration status: $_" -ForegroundColor DarkYellow
|
}
|
||||||
}
|
}
|
||||||
|
Write-Host "Installing printer asset..." -ForegroundColor Yellow
|
||||||
# 4. Create the Printer Port (Checks if it exists first to avoid duplicates)
|
try {
|
||||||
Write-Host "Creating printer port: $PortName..." -ForegroundColor Yellow
|
Add-Printer -DriverName $DriverName -Name $PrinterName -PortName $PortName -ErrorAction Stop
|
||||||
if (Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue) {
|
Write-Host "Successfully installed '$PrinterName' on port $PortName!" -ForegroundColor Green
|
||||||
Write-Host "Port $PortName already exists. Skipping port creation." -ForegroundColor Green
|
} catch {
|
||||||
} else {
|
Write-Error "Failed to install the printer: $_"
|
||||||
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: $_"
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user