commit a560094e3481ae121b3490f22cc079fdd87a2f99 Author: cousclou Date: Fri Aug 9 18:42:44 2024 +0200 auto diff --git a/printv1/add_printers.ps1 b/printv1/add_printers.ps1 new file mode 100644 index 0000000..2020fb3 --- /dev/null +++ b/printv1/add_printers.ps1 @@ -0,0 +1,85 @@ +Start-Transcript -Path c:\windows\temp\printers.log +#Read printers.csv as input +$Printers = Import-Csv .\printers.csv + +#Add all printer drivers by scanning for the .inf files and installing them using pnputil.exe +$infs = Get-ChildItem -Path . -Filter "*.inf" -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Fullname + +$totalnumberofinfs = $infs.Count +$currentnumber = 1 +Write-Host ("[Install printer driver(s)]`n") -ForegroundColor Green +Foreach ($inf in $infs) { + Write-Host ("[{0}/{1}] Adding inf file {2}" -f $currentnumber, $totalnumberofinfs, $inf) -ForegroundColor Green + try { + c:\windows\sysnative\Pnputil.exe /a $inf | Out-Null + } + catch { + try { + c:\windows\system32\Pnputil.exe /a $inf | Out-Null + } + catch { + C:\Windows\SysWOW64\pnputil.exe /a $inf | Out-Null + } + } + $currentnumber++ +} + +#Add all installed drivers to Windows using the CSV list for the correct names +$totalnumberofdrivers = ($printers.drivername | Select-Object -Unique).count +$currentnumber = 1 +Write-Host ("`n[Add printerdriver(s) to Windows]") -ForegroundColor Green +foreach ($driver in $printers.drivername | Select-Object -Unique) { + Write-Host ("[{0}/{1}] Adding printerdriver {2}" -f $currentnumber, $totalnumberofdrivers, $driver) -ForegroundColor Green + Add-PrinterDriver -Name $driver + $currentnumber++ +} + +#Loop through all printers in the csv-file and add the Printer port, the printer and associate it with the port and set the color options to 0 which is black and white (1 = automatic and 2 = color) +$totalnumberofprinters = $Printers.Count +$currentnumber = 1 +Write-Host ("`n[Add printer(s) to Windows]") -ForegroundColor Green +foreach ($printer in $printers) { + Write-Host ("[{0}/{1}] Adding printer {2}" -f $currentnumber, $totalnumberofprinters, $printer.Name) -ForegroundColor Green + #Set options for adding printers and their ports + $PrinterAddOptions = @{ + ComputerName = $env:COMPUTERNAME + Comment = $Printer.Comment + DriverName = $Printer.DriverName + Location = $Printer.Location + Name = $Printer.Name + PortName = $Printer.Name + } + + $PrinterConfigOptions = @{ + Color = 0 + DuplexingMode = 'TwoSidedLongEdge' + PrinterName = $Printer.Name + } + + $PrinterPortOptions = @{ + ComputerName = $env:COMPUTERNAME + Name = $Printer.Name + PrinterHostAddress = $Printer.PortName + PortNumber = '9100' + } + + #Check if the printer already exists and remove it + $existingPrinter = Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Name = '$($printer.Name)'" + if ($existingPrinter -ne $null) { + $existingPrinter.Delete() + } + + #Check if the printer port already exists and remove it + $existingPort = Get-WmiObject -Query "SELECT * FROM Win32_TCPIPPrinterPort WHERE Name = '$($printer.Name)'" + if ($existingPort -ne $null) { + $existingPort.Delete() + } + + #Add printer port and configure it with the required options + Add-PrinterPort @PrinterPortOptions + Add-Printer @PrinterAddOptions -ErrorAction SilentlyContinue + Set-PrintConfiguration @PrinterConfigOptions + + $currentnumber++ +} +Stop-Transcript \ No newline at end of file diff --git a/printv1/install.cmd b/printv1/install.cmd new file mode 100644 index 0000000..b95f72e --- /dev/null +++ b/printv1/install.cmd @@ -0,0 +1 @@ +powershell.exe -executionpolicy bypass -file .\add_printers.ps1 \ No newline at end of file diff --git a/printv1/printers.csv b/printv1/printers.csv new file mode 100644 index 0000000..c98f60f --- /dev/null +++ b/printv1/printers.csv @@ -0,0 +1,3 @@ +"Name","DriverName","PortName","Comment","Location" +"COPIEUR_CANON 3E ETAGE[PAF]","Canon Generic PCL6 V4","10.46.10.201","S93","PAF" +"COPIEUR_CANON 2E ETAGE[PAF]","Canon Generic PCL6 V4","10.46.10.200","S93","PAF" \ No newline at end of file diff --git a/printv1/remove_printers.ps1 b/printv1/remove_printers.ps1 new file mode 100644 index 0000000..11bd071 --- /dev/null +++ b/printv1/remove_printers.ps1 @@ -0,0 +1,33 @@ +#Read printers.csv as input +$Printers = Import-Csv .\printers.csv + +#Loop through all printers in the csv-file and remove the printers listed +foreach ($printer in $Printers) { + #Set options + $PrinterRemoveOptions = @{ + Confirm = $false + Name = $printer.Name + } + + $PrinterPortRemoveOptions = @{ + Confirm = $false + Computername = $env:COMPUTERNAME + Name = $printer.Name + } + + #Remove printers and their ports + Get-WmiObject -Class Win32_Printer | Where-Object { $_.Name -eq $printer.Name } | ForEach-Object { $_.Delete() } + Start-Sleep -Seconds 10 + Get-WmiObject -Class Win32_TCPIPPrinterPort | Where-Object { $_.Name -eq $printer.Name } | ForEach-Object { $_.Delete() } +} + +#Remove drivers from the system +foreach ($driver in $Printers.drivername | Select-Object -Unique) { + $PrinterDriverRemoveOptions = @{ + Confirm = $false + Computername = $env:COMPUTERNAME + Name = $driver + RemoveFromDriverStore = $true + } + Remove-PrinterDriver @PrinterDriverRemoveOptions +} \ No newline at end of file diff --git a/printv1/uninstall.cmd b/printv1/uninstall.cmd new file mode 100644 index 0000000..6471032 --- /dev/null +++ b/printv1/uninstall.cmd @@ -0,0 +1 @@ +powershell.exe -executionpolicy bypass -file .\remove_printers.ps1 \ No newline at end of file