2

I'm trying to create network printers using a Powershell script. The script below creates the port without any issues, but will not create the queue. Can you anyone confirm if this script works on Windows Server 2008? (Note, you need to have the driver installed in order for this to work).

function CreatePrinterPort {
Param (
 [string]$IPAddress
)

$port = [wmiclass]"Win32_TcpIpPrinterPort"
$newPort = $port.CreateInstance()
$newport.Name= "IP_$IPAddress"
$newport.SNMPEnabled=$false
$newport.Protocol=1
$newport.HostAddress= $IPAddress
Write-Host "Creating Port $ipaddress" -foregroundcolor "green"
$newport.Put()
}

function CreatePrinter {
    Param (
    [string]$PrinterName,
    [string]$DriverName,
    [string]$IPAddress,
    [string]$Location,
    [string]$Comment
    )

$print = [WMICLASS]"Win32_Printer"
$newprinter = $print.createInstance()
$newprinter.Drivername = $DriverName
$newprinter.PortName = "IP_$IPAddress"
$newprinter.Shared = $true
$newprinter.Sharename = $PrinterName
$newprinter.Location = $Location
$newprinter.Comment = $Comment
$newprinter.DeviceID = $PrinterName
Write-Host "Creating Printer $printername" -foregroundcolor "green"
$newprinter.Put()

}

CreatePrinterPort -IPAddress "Localhost"

CreatePrinter  -PrinterName Print1 -DriverName "HP LaserJet 4" -PortName "Localhost"`
                -Location "Office" -Comment "Test comment"

The error I'm getting is on the CreatePrinter function:

Exception calling "Put" with "0" argument(s): "Generic failure "

fenster
  • 454
  • 4
  • 13
  • getting exactly the same problem with this. did you ever get to the bottom of it? –  Mar 16 '11 at 17:14

2 Answers2

3

Shouldn't your PortName be "IP_$IPAddress" instead of "Localhost"?

CreatePrinter  -PrinterName Print1 -DriverName "HP LaserJet 4" -PortName "IP_123.123.123.123" -Location "Office" -Comment "Test comment"

Furthermore, your DriverName needs to be the exact name for that driver. You don't get to choose it; it's specified by the manufacturer.

pk.
  • 6,413
  • 1
  • 41
  • 63
  • In nearly all cases the port name is arbitrary and can be anything at all. You're spot on about the driver name though. – John Gardeniers Mar 21 '11 at 20:50
  • @John: The CreatePrinter function needs to mirror the port name assigned in the CreatePrinterPort function, no? He specifies it as "IP_$IPAddress" in the CreatePrinterPort function, so he should stick with it on the CreatePrinter call. – pk. Mar 21 '11 at 20:54
  • that's what I used to think as well but I long ago discovered that isn't the case at all, except where software addresses the printer by port, which is generally only high end printers that run multiple ports simultaneously. – John Gardeniers Mar 21 '11 at 20:58
0

The problem with your script is that you declare $IPAddress in your function but specify -portname when you call the function. Either change the function to use $PortName or use -IPAddress when calling the function.

Personally I changed your function to use [string]$PortName

Here is your function working properly

  function CreatePrinter {
    Param (
    [string]$PrinterName,
    [string]$DriverName,
    [string]$PortName,
    [string]$Location,
    [string]$Comment
    )

$print = [WMICLASS]"Win32_Printer"
$newprinter = $print.createInstance()
$newprinter.Drivername = $DriverName
$newprinter.PortName = "IP_$PortName"
$newprinter.Shared = $true
$newprinter.Sharename = $PrinterName
$newprinter.Location = $Location
$newprinter.Comment = $Comment
$newprinter.DeviceID = $PrinterName
Write-Host "Creating Printer $printername" -foregroundcolor "green"
$newprinter.Put()

}

$printerport1 = "10.10.10.0"

CreatePrinterPort -IPAddress $printerport1

CreatePrinter  -PrinterName "Print1" -DriverName "HP LaserJet 4300 PCL 6" -PortName $printerport1 -Location "Office" -Comment "Test comment"