2

I have a legacy application that is printing, and I cannot disable this in the application. This task prints to a Windows defined printer queue. The issue I run into is that without a real printer attached, eventually the print queue gets full and then the jobs that are printing stop, waiting for the queue to respond that the job was accepted.

The legacy application is printing via the Powershell scripting.

if( sm_control_flag )
{
    get-content $TO_PRINTER | Out-Printer -name $cfgs.PRINTER_QUEUE
}

The problem is that these scripts cannot be changed due to them being in production in distributed locations. There are a number of scripts that do this sort of process.

The Out-Printer needs to return a success, not an error, or the script will abort with a printing error. Therefore, the queue must exist (which is basically a printer defined in the Windows Server OS).

If there is not a real printer defined and attached to the queue, Windows will accept the job and queue the output to print. The issue is that the queue will eventually fill up the free space on the server, and then block all printing. The Powershell script's Out-Printer will also pause and wait for queue space to be freed up when the queue is full.

I cannot disable the printing on the computer and delete jobs, as other printers are connected and need to print. What I need is a printer driver that simply accepts the print job, but does nothing with it. I do not need it to print, just be deleted.

I was also trying to simply print to a file, so I could reuse same file name each time, which would overwrite the last print output, and meet my needs. The problem is that printing to a file always seems to prompt the user for the file name. This being a Windows server, there is no user to actually prompt for a file name from, so this blocks the queue and pauses the script as well.

Does anyone know of a way to accept the jobs and then simply delete the output and not print the job?

6
  • I need the queue to accept the job, otherwise an error is returned in the current printing logic to the queue (Powershell print needs to return a 0 for success). The PDF would work except we always seem to get prompted for a name (headless computer, no one to enter a name) instead of just being able to a set a constant name which would even get rid of the need to delete the file. Commented Apr 23, 2018 at 14:40
  • Can you direct the output to a virtual PDF printer? Then just delete the PDF's periodically.
    – cybernard
    Commented Apr 24, 2018 at 14:20
  • "The problem is that these scripts cannot be changed due to them being in production in distributed locations." That does not make sense to me. If you can install a new printer driver to the computers in question, why can't you change a few dinky Powershell scripts?
    – sleske
    Commented Apr 24, 2018 at 20:34
  • @sleske In this production environment, the customer has different teams. While they will not change the running code as it involves changes to many software vendor systems, a new default printer in the environment is handled by a different team, and only impacts that team. With the software distributed into thousands of locations, the printer driver can be pushed out without needing various software vendors, including ourselves, to make changes. Commented Apr 25, 2018 at 13:35
  • 1
    No need to use a script for this. Windows has a built-in nul: printer port that does exactly what the OP wants. Just map any printer driver to it, give the queue a name, and he'll have the print queue he needs listed in the system. Commented Jun 1, 2018 at 19:48

2 Answers 2

2

Open the Control Panel and choose Devices and Printers.

When the Device and Printers window opens, choose Add Printer. What comes next depends on your network and what edition of Windows you have, but you may need to choose The printer I want isn't listed.

At this point, you should be on a window with a radio button to Add a local printer or network printer with manual settings. Choose that option and then click Next.

From here, you can Use an existing port: and choose nul: (Local Port) from the drop down list.

Click Next again and now you can choose just about any printer driver you want, give the printer a name, and finish the setup.

The printer you just created will be a black hole on the system. It will process print jobs using the driver you selected, including clearing the job from the print queue, but the output will go nowhere.

2
  • 1
    That seems very promising, but I do not have the nul:(Local Port) option on my computer. This is likely something from the domain, etc. However, on another server, I do have this port, and this does solve the issue exactly as I needed. Thank you. Commented Jun 4, 2018 at 14:04
  • Nice idea Joel!! Commented Jun 10, 2018 at 17:41
1
strPort="<printer port name"
strNameSpace= "root\cimv2"
#the sql/wql query can be changed to go on other values.

#prepare to connect to a remote computer
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
#connect to a remote computer 
#strUser local or domain admin username
#strPassword password for above
Set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
#Set administrator privileges
oService.Security_.impersonationlevel = 3
oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
#Query the internal representation of the printer
Set oPrinter=oService.ExecQuery("SELECT * FROM Win32_Printer where DeviceID like'"&strPort&"%'")
#alternative query option
#Set oPrinter=oService.ExecQuery("SELECT * FROM Win32_Printer where Name like'"&strPort&"%'")
#If you give the strPort and exact name only 1 printer will match
#It can match multiple printers if two printer have similiar names
# printer:  me-1 and me-2 with both be matched by strPort="me"
For Each Printer In oPrinter
#pause the printer
uResult = Printer.Pause()
#cancel all jobs
uResult = Printer.CancelAllJobs()
#if any error occures uResult will not be 0
if uResult <>0 then wscript.echo "Error occured"
Next

If you run the script on the computer with the print queue you may not need to have credentials.

Save as a vbs file cscript whatever.vbs

3
  • It's nice that you took the time to write a script. Could you add a short explanation what it does, and how, for others who don't know VBS? I can guess that it cancels all print jobs, but I'm not sure.
    – sleske
    Commented Apr 25, 2018 at 17:13
  • @sleske added notes. The script deletes all print jobs from specified printer.
    – cybernard
    Commented Apr 25, 2018 at 19:09
  • While this script works, for ease of deployment and use, the @Joel Coehoorn answer with the nul: (Local Port) setting is exactly what we were looking for. I had to change the accepted answer to Joel's but do want to point out that this script does work and solves the problem. Commented Jun 4, 2018 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .