diff --git a/printv2/add_printers.ps1 b/printv2/add_printers.ps1 new file mode 100644 index 0000000..83300ea --- /dev/null +++ b/printv2/add_printers.ps1 @@ -0,0 +1,115 @@ +[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("ISO-8859-1") + +# Lancer la transcription +Start-Transcript -Path c:\windows\temp\printers.log + +#Read printers.csv as input +$Printers = Import-Csv .\printers.csv + +# Afficher la liste des imprimantes disponibles +Write-Host "Imprimantes disponibles :" +for ($i = 0; $i -lt $Printers.Count; $i++) { + Write-Host "$($i + 1). $($Printers[$i].Name)" +} + +# Laisser l'utilisateur choisir les imprimantes à installer (séparées par des virgules) +$choices = Read-Host "Entrez le numero des imprimantes a installer (separes par des virgules)" + +$selectedPrinters = @() +$selectedChoices = $choices -split "," +foreach ($choice in $selectedChoices) { + $choice = $choice.Trim() + if ($choice -match '^\d+$' -and [int]$choice -ge 1 -and [int]$choice -le $Printers.Count) { + $selectedPrinters += $Printers[[int]$choice - 1] + } +} + +#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 selected printer drivers to Windows using the CSV list for the correct names +$selectedDriverNames = $selectedPrinters | Select-Object -ExpandProperty DriverName | Select-Object -Unique +$totalnumberofdrivers = $selectedDriverNames.Count +$currentnumber = 1 +Write-Host ("`n[Add selected printerdriver(s) to Windows]") -ForegroundColor Green +foreach ($driverName in $selectedDriverNames) { + Write-Host ("[{0}/{1}] Adding printerdriver {2}" -f $currentnumber, $totalnumberofdrivers, $driverName) -ForegroundColor Green + Add-PrinterDriver -Name $driverName + $currentnumber++ +} + +#Loop through selected printers 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 = $selectedPrinters.Count +$currentnumber = 1 +Write-Host ("`n[Add selected printer(s) to Windows]") -ForegroundColor Green +foreach ($printer in $selectedPrinters) { + 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 + + # Afficher un message de succès + Write-Host ("Imprimante {0} installee avec succes!" -f $printer.Name) -ForegroundColor Green + + $currentnumber++ +} + +Start-Sleep -Seconds 3 + +# Arrêter la transcription +Stop-Transcript diff --git a/printv2/install.cmd b/printv2/install.cmd new file mode 100644 index 0000000..cca8c99 --- /dev/null +++ b/printv2/install.cmd @@ -0,0 +1,4 @@ +chcp 1252 > nul +pushd %~dp0 +powershell.exe -executionpolicy bypass -file .\add_printers.ps1 + diff --git a/printv2/printers.csv b/printv2/printers.csv new file mode 100644 index 0000000..4e83e9c --- /dev/null +++ b/printv2/printers.csv @@ -0,0 +1,7 @@ +"Name","DriverName","PortName","Comment","Location" +"KONICA-A","KONICA MINOLTA Universal PCL","10.0.1.20","GOOGLE","A" +"KONICA-B","KONICA MINOLTA Universal PCL","10.0.1.22","GOOGLE","B" +"KONICA-C","KONICA MINOLTA Universal PCL","10.0.1.21","GOOGLE","C" +"KONICA-D","KONICA MINOLTA Universal PCL","10.0.3.20","GOOGLE","D" +"KONICA-E","KONICA MINOLTA Universal PCL","10.0.3.21","GOOGLE","E" + diff --git a/printv2/remove_printers.ps1 b/printv2/remove_printers.ps1 new file mode 100644 index 0000000..304fda0 --- /dev/null +++ b/printv2/remove_printers.ps1 @@ -0,0 +1,38 @@ +# Afficher la liste des imprimantes installées +Write-Host "Imprimantes installees :" +$installedPrinters = Get-WmiObject -Query "SELECT * FROM Win32_Printer" | Select-Object Name, DriverName, PortName | Sort-Object Name +for ($i = 0; $i -lt $installedPrinters.Count; $i++) { + Write-Host "$($i + 1). $($installedPrinters[$i].Name)" +} + +# Laisser l'utilisateur choisir les imprimantes à désinstaller (séparées par des virgules) +$choices = Read-Host "Entrez le numero des imprimantes a desinstaller (separes par des virgules)" + +$selectedChoices = $choices -split "," +$successMessages = @() + +foreach ($choice in $selectedChoices) { + $choice = $choice.Trim() + if ($choice -match '^\d+$' -and [int]$choice -ge 1 -and [int]$choice -le $installedPrinters.Count) { + $selectedPrinter = $installedPrinters[[int]$choice - 1] + + # Supprimer l'imprimante et son port associé + Write-Host "Desinstallation de l'imprimante $($selectedPrinter.Name) en cours..." -ForegroundColor Yellow + Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Name = '$($selectedPrinter.Name)'" | ForEach-Object { $_.Delete() } + Start-Sleep -Seconds 10 + Get-WmiObject -Query "SELECT * FROM Win32_TCPIPPrinterPort WHERE Name = '$($selectedPrinter.Name)'" | ForEach-Object { $_.Delete() } + + # Ajouter un message de succès + $successMessages += "L'imprimante $($selectedPrinter.Name) a ete desinstallee avec succes." + } else { + Write-Host "Choix invalide : $choice. Ignore." -ForegroundColor Red + } +} + +# Afficher les messages de succès +foreach ($message in $successMessages) { + Write-Host $message -ForegroundColor Green +} + +# Pause de 3 secondes +Start-Sleep -Seconds 3 diff --git a/printv2/uninstall.cmd b/printv2/uninstall.cmd new file mode 100644 index 0000000..9a69504 --- /dev/null +++ b/printv2/uninstall.cmd @@ -0,0 +1,2 @@ +pushd %~dp0 +powershell.exe -executionpolicy bypass -file .\remove_printers.ps1 \ No newline at end of file