Scripting Printers and the FOR command

Recently, I have found myself having to do some scripting with printers (among other things).  As I was researching the best way to do this, I ran across some vbs scripts that Microsoft includes with at least Server 2003 (and it looks like 2008, as well).  Here are some of the scripts I found:

  • prncnfg.vbs – Configures or displays configuration information about a printer.
  • prndrvr.vbs – Adds, deletes, and lists printer drivers.
  • prnjobs.vbs – Pauses, resumes, cancels, and lists print jobs.
  • prnmngr.vbs – Adds, deletes, and lists printers or printer connections, in addition to setting and displaying the default printer.
  • prnport.vbs – Creates, deletes, and lists standard TCP/IP printer ports, in addition to displaying and changing port configuration.
  • prnqctl.vbs – Prints a test page, pauses or resumes a printer, and clears a printer queue.
  • pubprn.vbs – Publishes a printer to the Active Directory directory service.

(For complete details on these commands, go to http://technet.microsoft.com/en-us/library/cc771846.aspx ).

One of the things that I needed to do was update the location on all the printers on a print server.  The above printer commands are excellent for being able to update this information.  For example, here would be the command that I would run to update a printer location:

cscript prncnfg.vbs -t -p printername -l "Location/Code/Here"

Unfortunately, there were 299 printers on this print server.  The above printer commands are excellent for being able to update this, but don’t allow for the inputting of a csv file to run it multiple times.  The other unfortunate part is that this was not the only set of commands that I wanted to run against all of these printers.  So, how do I do this without having to reinvent the wheel?

Here is where the FOR command comes in handy. It is a command that allows us to pipe in a file to a command line function.  Here is an example:

FOR /f "tokens=1,2" delims=," %a IN (prnlocations.txt) DO cscript prncnfg.vbs –t –p %a –l %b

Let me explain this command a little.  The /f”tokens=1,2” delims=,” %a portion of the command tells FOR that we are using the first two tokens returned and that we have a deliminter of a comma (instead of the default tab or space).  The file name in () is the name of the input file.  After the DO command is the actual command to be run.  Here you can see that I am inputting two variables – one for the printername and one for the location code. 

This same command can be used whenever you want to take a script or a command that allows variables to be passed on to it via the command line.  Now, I’m off to using the FOR command on dnscmd.exe…

(One note that I might include for you here is that if you are using this in a batch file, you need to change the variable (the %a and %b above) to %%a.  You might want to read the full documentation available here:  http://technet.microsoft.com/en-us/library/cc754900.aspx .  Thanks also to http://blog.geekpoet.net/2008/04/quick-and-dirty-way-to-mass-update.html for getting me started in this direction.)