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
(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.