Link to home
Start Free TrialLog in
Avatar of michalek19
michalek19Flag for United States of America

asked on

Powershell script that would remove printers from print servers base on CSV file and add new printer objects to print servers

Hi

I am looking for two  Powershell scripts
 
First PS script should remove printers objects from print servers " c:\temp\servers.txt"
Second PS Script should add new printers objects to printer servers.

1. PS script should  pull data from CSV file "Corppr2.csv",  this file will contain all information about printers that  need to remove or add.
2. PS script should  content -path from c:\temp\servers.txt
3. PS script should run against any print servers from that "servers.txt" list and execute script to remove or add printers
4. Output file" Log file" should be generated

File "corppr2.csv" is the  source of printer objects  that would need to be attached to Powershell script for execution

Please help, Thank you Mike
Avatar of michalek19
michalek19
Flag of United States of America image

ASKER

OK, let me try to put some staff together
File server name should be pull from the  txt file ($Printservers = Get-Content  ( server.txt))
CSV file should be pull from  $InputFile =  CSV file

This is CSV file and its format
Printserver         Driver          Portname      IPAddress Sharename      Location Commen  Printername  Bidirectional  BannerSheet
xxxxx Xerox WorkCentre 7435 PCL6      TRUE 10.75.65.54       Only 7435 Room3  BlackWihte  Only 7435  Disable  Disable
xxxxx Xerox WorkCentre 7435 PCL7      TRUE 10.74.65.231Test  7436 Room4  ColorPrinter Test  7436 Disable Disable


Here is part of the script to add printers objects that works but is missing function for BiDirectional and Bannersheet
------------------------------------------------------------------------------------------------------------------------
$DebugPreference = “silentlyContinue”

$printers = Import-Csv c:\temp2\printers.csv

function CreatePrinter
{
$server = $args[0]
$print = ([WMICLASS]”\\$server\ROOT\cimv2:Win32_Printer”).createInstance()
$print.drivername = $args[1]
$print.PortName = $args[2]
$print.Shared = $true
$print.Sharename = $args[3]
$print.Location = $args[4]
$print.Comment = $args[5]
$print.DeviceID = $args[6]
$print.PrintProcessor = $args[7]
$print.Rawonly = $true
$print.DoCompleteFirst = $true
$print.Put()
}

function CreatePrinterPort
{
$server = $args[0]
$port = ([WMICLASS]”\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort”).createInstance()
$port.Name= $args[1]
$port.SNMPEnabled=$false
$port.Protocol=1
$port.HostAddress= $args[2]
$port.Put()
}

foreach ($printer in $printers)
{
CreatePrinterPort $printer.Printserver $printer.Portname $printer.IPAddress
CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.Printername $printer.PrintProcessor
}
---------------------------------------------------------------------------------------------------------------------------------------------------

I don't have script to remove printobjects

Please let me know if this is to many information or if is not clear

Thx, Mike
Createprintobject.txt
Printers.csv
Avatar of Bob Learned
Since that uses the Win32_Printer class from WMI, enabling bidirectional is in the EnableBidi property:

Win32 Printer Class
https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

I have no idea about BannerSheet, though.
I need help to modify printers removal script to pull printers from CSV  and pull  print server names from '.txt" file.
CSV file should have

This is CSV file and its format
Printserver         Driver          Portname      IPAddress Sharename      Location Commen  Printername  Bidirectional  
xxxxx            Xerox WorkCentre 7435 PCL6      TRUE 10.75.65.54       Only 7435 Room3  BlackWihte  Only 7435  Disable  

Script
$PrintersTodelete = Get-WmiObject -Class Win32_printer -ComputerName $server -filter "name='printer0456'"
 if($printersToDelete){
     Foreach($printer in $PrintersTodelete){
         $printer.delete()
      }
 }
You have a lot going on in this question, so it was confusing what your main question is...

Did you figure out the solution to your own question?
i have a script to disable bidirectional.

I am still mot getting any luck with printer objects removal.  

Do you have any example of the script that would pull printers from csv file and remove them on print server?
"I am still not getting any luck with printer objects removal."
What issues are you having?

I don't have any example script, but I am willing to help you get over your issues.
So, here is a script. When i ran this script nothing happen to printer object

$Source = "C:\temp2\rmprinters.csv"
#-----Create object to use data in the csv file-----#
$printers = Import-Csv $Source
#$printers = Import-Csv c:\temp2\

function CreatePrinterPort     {      
$port = ([WMICLASS]"\\LocalHost\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()      
$port.Name = $args[1]      
$port.SNMPEnabled = $false      
$port.Protocol = 1
$port.HostAddress = $args[2]    }
#-----create function block to RECREATE the PRINTER object-----#
function CreatePrinter     {      
$print = ([WMICLASS]"\\LocalHost\ROOT\cimv2:Win32_Printer").createInstance()        
$print.drivername = $args[1]      
$print.PortName = $args[2]      
$print.Shared = $true      
$print.Sharename = $args[3]      
$print.Location = $args[4]      
$print.Comment = $args[5]      
$print.DeviceID = $args[6]      
$print.Rawonly = $true      
$print.DoCompleteFirst = $true    }
#-----Foreach loop to Call the function blocks and declare which properties, that are outlined in the CSV, will be used by the script.-----#
foreach ($printer in $printers)     {      
<#DeletePrinter $printer.Printserver $printer.Printername        DeletePrinterPort $printer.Printserver $printer.Portname#>
CreatePrinterPort
$printer.Printserver
$printer.Portname
$printer.HostAddress      
CreatePrinter
$printer.Printserver
$printer.Driver
$printer.Portname
$printer.Sharename
$printer.Location
$printer.Comment
$printer.Printername     }
#-----END-----#
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial