1

I am trying to prepare a PowerShell script to install printers. This will be for an automated task where printer names and IP addresses will be provided as available. Most of the script appears to work, but something is missing. Below is the script, then I'll clarify missing.

$knownPrinterDriver = "DTC4500e Card Printer"
$ipPort = 5400
$finalPortName = "MyPort_5400"
$ipAddress = "192.168.30.13"
$finalPrinterName = "Printer1"

$goodDriver = Get-PrinterDriver | Where-Object { $_.Name -eq $knownPrinterDriver }
if( $goodDriver.Count -eq 0 )
{
   # no printer driver found
   return -1
}

# SHOULD only be ONE, so get it as reference    
$oneDriver = $goodDriver[0]

# using the ` backtick mark as continuation line
Add-PrinterPort `
   -Name $finalPortName  `
   -PrinterHostAddress $ipAddress  `
   -PortNumber $ipPort

# Re-check if printer was actually loaded or not.
$availPorts = Get-PrinterPort | Where-Object { $_.Name -eq $finalPortName }
if( $availPorts.Count -eq 0 )
{
   return -2
}

$onePort = $availPorts[0]


Add-Printer `
   -DriverName $oneDriver.Name `
   -Name $finalPrinterName `
   -PortName $onePort.Name

# Re-check if printer was actually loaded or not.
$availPrinters = Get-Printer | Where-Object { $_.Name -eq $finalPrinterName }
if( $availPrinters.Count -eq 0 )
{
   return -3
}

return 1

So, after running the script, it creates the port, no problem. Creates the printer associated to the port, no problem.

Now, I go to Printers & Settings

Results of printer

(obviously edited to show both) You can see the one printer shows the option to remove device, the scripted one does not. Also, second part. While trying to run other program polling the device, the status is "unknown" vs ready or offline.

Suggestions? Appreciate it.

DRapp
  • 113
  • 7

1 Answers1

1

Question 1

In your cmdlet Add-PrinterPort you miss the SNMP part, it's that parameter that will make the spooler poke the printer for status. (Disabling the SNMP part will disable too any print monitor software that come with the driver)

-SNMP Enables SNMP and specifies the index for TCP/IP printer port management.

-SNMPCommunity Specifies the SNMP community name for TCP/IP printer port management.

Question 2

Your script create a local TCP/IP port, thus only an admin can remove it or not, please check the user right/UAC, as I suspect the other printer is a mapped printer, not a local printer.

yagmoth555
  • 16,758
  • 4
  • 29
  • 50
  • Looking at the .SNMP option, what "index" value do I assign it. I don't know which Windows Index TCP/IP it would reference. For remove printer, makes possible sense as I am running script As Admin. Both printers are local. – DRapp Nov 21 '18 at 19:06
  • @DRapp By default the index is '1', and the Community is 'public', it will add those option to the port the command create – yagmoth555 Nov 21 '18 at 19:21
  • Although not your issue, and I will probably check as answered, just having problems with this as part of it is an emulator, and the other part is a second printer is using the same port and is apparently hanging on the script. – DRapp Nov 21 '18 at 19:54