diff --git a/Tasks/Check-Repair-Printers.ps1 b/Tasks/Check-Repair-Printers.ps1 new file mode 100644 index 0000000..b4e35a7 --- /dev/null +++ b/Tasks/Check-Repair-Printers.ps1 @@ -0,0 +1,141 @@ +# 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] +} \ No newline at end of file diff --git a/Tasks/System-Tasks/Check-Repair-Printers.bat b/Tasks/System-Tasks/Check-Repair-Printers.bat new file mode 100644 index 0000000..6aa910c --- /dev/null +++ b/Tasks/System-Tasks/Check-Repair-Printers.bat @@ -0,0 +1,2 @@ +@echo off +powershell.exe -ExecutionPolicy Bypass -File "c:\scripts\Tasks\Check-Repair-Printers.ps1"