141 lines
5.5 KiB
PowerShell
141 lines
5.5 KiB
PowerShell
# Define printer information in a hashtable for easy management
|
|
$printers = @{
|
|
'Aspire Admin (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera CS 306ci KX"
|
|
PrinterPort = "10.4.2.51"
|
|
PrinterPortName = "10.4.2.51"
|
|
}
|
|
'Aspire Hallway (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.54"
|
|
PrinterPortName = "10.4.2.54"
|
|
}
|
|
'Aspire Lobby (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.52"
|
|
PrinterPortName = "10.4.2.52"
|
|
}
|
|
'Aspire Office (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.53"
|
|
PrinterPortName = "10.4.2.53"
|
|
}
|
|
'Center Annex (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera CS 306ci KX"
|
|
PrinterPort = "10.4.2.58"
|
|
PrinterPortName = "10.4.2.58"
|
|
}
|
|
'Center Hallway (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.56"
|
|
PrinterPortName = "10.4.2.56"
|
|
}
|
|
'Center Office (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.55"
|
|
PrinterPortName = "10.4.2.55"
|
|
}
|
|
'Center Psych (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.57"
|
|
PrinterPortName = "10.4.2.57"
|
|
}
|
|
'DQ Annex (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.59"
|
|
PrinterPortName = "10.4.2.59"
|
|
}
|
|
'Ward Hallway (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.61"
|
|
PrinterPortName = "10.4.2.61"
|
|
}
|
|
'Ward Office (Kyocera)' = @{
|
|
DriverPath = "C:\Scripts\Printers\00-Kyocera-Driver\KX852222\64bit\OEMSETUP.INF"
|
|
DriverName = "Kyocera TASKalfa 5054ci KX"
|
|
PrinterPort = "10.4.2.60"
|
|
PrinterPortName = "10.4.2.60"
|
|
}
|
|
}
|
|
|
|
# Function to check if printer exists with the same name, driver, and port
|
|
function Check-PrinterExists {
|
|
param(
|
|
[string]$PrinterName,
|
|
[string]$DriverName,
|
|
[string]$PortName
|
|
)
|
|
|
|
$existingPrinters = Get-Printer | Where-Object {
|
|
$_.Name -eq $PrinterName -and
|
|
$_.DriverName -eq $DriverName -and
|
|
$_.PortName -eq $PortName
|
|
}
|
|
|
|
return ($existingPrinters | Measure-Object).Count -gt 0
|
|
}
|
|
|
|
# Function to handle printer installation or reinstallation
|
|
function Install-Printer {
|
|
param(
|
|
[string]$PrinterName,
|
|
[hashtable]$PrinterInfo
|
|
)
|
|
|
|
$driverPath = $PrinterInfo.DriverPath
|
|
$driverName = $PrinterInfo.DriverName
|
|
$printerPort = $PrinterInfo.PrinterPort
|
|
$printerPortName = $PrinterInfo.PrinterPortName
|
|
|
|
# Check if the printer with the exact configuration already exists
|
|
if (Check-PrinterExists -PrinterName $PrinterName -DriverName $driverName -PortName $printerPortName) {
|
|
Write-Warning "Printer with the same name, driver, and port already exists."
|
|
return
|
|
}
|
|
|
|
# Check if the printer with the same name exists but with different configuration
|
|
$existingPrinter = Get-Printer | Where-Object { $_.Name -eq $PrinterName }
|
|
if ($existingPrinter) {
|
|
Write-Warning "Printer with the same name exists, but configuration differs. Deleting and reinstalling."
|
|
Remove-Printer -Name $PrinterName -Confirm:$false
|
|
}
|
|
|
|
# Install the driver if not already installed
|
|
if ($null -eq (Get-PrinterDriver -name $driverName -ErrorAction SilentlyContinue)) {
|
|
pnputil.exe /a $driverPath
|
|
Add-PrinterDriver -Name $driverName
|
|
} else {
|
|
Write-Warning "Printer driver already installed"
|
|
}
|
|
|
|
# Add printerPort if not already exists
|
|
if ($null -eq (Get-PrinterPort -name $printerPortName)) {
|
|
Add-PrinterPort -Name $printerPortName -PrinterHostAddress $printerPort
|
|
} else {
|
|
Write-Warning "Printer port with name $($printerPortName) already exists"
|
|
}
|
|
|
|
try {
|
|
# Add the printer
|
|
Add-Printer -Name $PrinterName -DriverName $driverName -PortName $printerPortName -ErrorAction stop
|
|
Write-Host "Printer '$PrinterName' successfully installed" -ForegroundColor Green
|
|
printUI.exe /Sr /n $PrinterName /a C:\Scripts\Printers\03-Printer-Properties\$PrinterName.dat d g
|
|
} catch {
|
|
Write-Host $_.Exception.Message -ForegroundColor Red
|
|
}
|
|
}
|
|
|
|
# Install each printer defined in the hashtable
|
|
foreach ($printerName in $printers.Keys) {
|
|
Install-Printer -PrinterName $printerName -PrinterInfo $printers[$printerName]
|
|
} |