Powershell Batch Creation of Printers

Yesterday I considered how to connect 70 printers scattered in different buildings in a print server. There are traditionally a few options:

  1. Create by hand.
  2. Use tools to migrate printers from multiple servers and then understand what happened
  3. Code a script ...

Of course I prefer the third ...

Therefore, this example focuses on how you can quickly and without much headache create a printer using a simple powershell script.

Task

From the information in a list create a port + printer + driver. You want to specify the description and location.

Decision

The solution was found on the Web literally within the first ten minutes of digging. It took ten more minutes for a test run and checking the results.

The first phase

Create a Script:

# function to create a printer

function CreatePrinter {

# Gets the name of the print server

$server = $ args[0]

# connect to WMI

$print = ([WMICLASS]"\server\ROOT\cimv2:Win32_Printer")createInstance.()

# driver name

$print $. drivername = $args [1]

# Gets the name of the port

$print.PortName = $ args[2]

# default rasšaren printer

$print.Shared = $true

# name balls

$print.Sharename = $args [3]

# location

$print.Location = $args [4]

# comment

$print.Comment = $args [5]

# device ID

$print.DeviceID = $args [6]

# write the result

$print.Put()

}

 

# function to create a port

function CreatePrinterPort {

# get the server name

$server = $args[0]

# connect to WMI

$port = ([WMICLASS]"\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()

$port.Name= $args[1]

# SNMP we don't need. Why do we SNMP …?

$port.SNMPEnabled=$ false

$port.Protocol=1

# address

$port.HostAddress= $args[2]

# write the result

$port.Put()

}

 

# import the data from a file

$printers = Import-Csv printers.csv

 

# obradatyvaem received data

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

}

 

The list of printers should be in the following format. If the field "comment" you description of it must be done in the encoding UTF8:

Printserver,Driver,Portname,IPAddress,Sharename,Location,Comment,Printername
OF-PRN,HP DeskJet 400,10.0.1.84_PR,10.0.1.84,HL-P001,test Location,test comment,HL-P001
OF-PRN,HP DeskJet 400,10.0.1.85_PR,10.0.1.84,HL-P002,test location,test comment,HL-P002
OF-PRN,HP DeskJet 400,10.0.1.86_PR,10.0.1.84,HL-P003,test location,test comment,HL-P003

You may receive the following script error:

I get the error «Exception calling «Put» with «0″ argument(s): «Generic failure «

In conclusion, let us examine the result.

  1. Ask for the list prinetrov

    get-wmiobject win32_printer -computer "prnsrv"

  2. Ask for a list of ports

    get-wmiobject win32_printer -computer "prnsrv" | %{$_.portname}

The second phase

To set up printers, you must assign permissions. In doing so, we help subinacl.exe utility, which can be found here.

Assign permissions as follows.

The tablet is right:

       F : Full Control
       M : Manage Documents
       P : Print

Now an example of assignment authority.

subinacl.exe /printer "\PRNSRV\PRN-01" /revoke="LAB\Fin Users"

subinacl.exe /printer "\PRNSRV\PRN-01" /grant="LAB\Group 01=MP"

By the way. This utility can be used to view permissions.

subinacl /verbose=1 /printer "\PRNSRV\PRN-01" /display

subinacl /verbose=1 /printer "\PRNSRV\" /display

Source Link