auto
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
powershell.exe -executionpolicy bypass -file .\add_printers.ps1
|
||||
@@ -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"
|
||||
|
@@ -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
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
powershell.exe -executionpolicy bypass -file .\remove_printers.ps1
|
||||
Reference in New Issue
Block a user