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
michalek19Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

michalek19Author Commented:
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
0
Bob LearnedCommented:
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.
0
michalek19Author Commented:
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()
      }
 }
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Bob LearnedCommented:
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?
0
michalek19Author Commented:
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?
0
Bob LearnedCommented:
"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.
0
michalek19Author Commented:
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-----#
0
Bob LearnedCommented:
1) Does $printers have > 0 printers

2) What Powershell version are you using?

3) I see that Powershell 4.0 has the Remove-Printer cmdlet for Windows Server 2012 R2 and Windows 8.1:

Remove-Printer
https://technet.microsoft.com/en-us/library/hh918356(v=wps.630).aspx

4) Have you seen this?

Hey, Scripting Guy! How Can I Use Windows PowerShell to Remove Old Printer Connections, List Printers, and Set New Default Printers?
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/20/how-can-i-use-windows-powershell-to-remove-old-printer-connections-list-printers-and-set-new-default-printers.aspx

Function Remove-Printer($printObject, [string]$printerPath)
{
 Write-Host -foregroundcolor cyan "Removing $printerPath"
  $dp = $printObject |
 Where-Object { $_.deviceID -match $printerPath }
 $dp.psbase.Delete()
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.