Wednesday, August 02, 2006

 

CreatePrinters.vbs

As previously mentioned, I wrote a script to loop through creation of multiple printers.

It makes a lot of assumptions, but does the trick. There are probably better ways to do some of the steps of this script, but again: it works.

CreatePrinters.vbs
' Creates printers on local system from input file
' Input file:
' - Must be plain text
' - No header or whitespace
' - Tab-delimited columns: Printer Name, IP Address, Driver Name, Location, Comment
' Assumptions:
' - The Standard TCP/IP Port will be named IP_%IPADDRESS% where %IPADDRESS% is the input value and it will use the RAW protocol on port 9100
' - The driver will be for Windows 2000/XP/2003 only and must be included with Windows source
' - The share name is the same as the printer name
' - The share will be published
' - The printer will use bi-directional support when available
' - Everything else is the default value
' The resulting printers are as if manually created accepting default values.

' create shell object (used later)
set objWsh = WScript.CreateObject ( "WScript.Shell" )

' create FileSystemObject
set objFSO = WScript.CreateObject ( "Scripting.FileSystemObject" )

' open file, create TextStream Object
set objTSO = objFSO.OpenTextFile ( "printers.txt" )

' loop through the file
Do Until objTSO.AtEndofStream

' read a line
strLine = objTSO.ReadLine

' split the line into an array
strArray = Split ( strLine, vbTab )

' build command lines
strTask1 = "cmd /c cscript %windir%\system32\prnport.vbs -a -r IP_" & strArray(1) & " -h " & strArray(1) & " -o raw -n 9100 > c:\" & strArray(0) & "-1.log"
strTask2 = "cmd /c cscript %windir%\system32\prndrvr.vbs -a -m """ & strArray(2) & """ -v 3 -e ""Windows NT x86"" > c:\" & strArray(0) & "-2.log"
strTask3 = "cmd /c cscript %windir%\system32\prnmngr.vbs -a -p """ & strArray(0) & """ -m """ & strArray(2) & """ -r IP_" & strArray(1) & " > c:\" & strArray(0) & "-3.log"
strTask4 = "cmd /c cscript %windir%\system32\prncnfg.vbs -t -p """ & strArray(0) & """ -l """ & strArray(3) & """ -m """ & strArray(4) & """ -h """ & strArray(0) & """ +shared +published +enablebidi > c:\" & strArray(0) & "-4.log"

' run the commands
objWsh.Run strTask1, 1, 1
objWsh.Run strTask2, 1, 1
objWsh.Run strTask3, 1, 1
objWsh.Run strTask4, 1, 1
Loop

' cleanup
objTSO.Close
set objFSO = Nothing

Sample Input File, printers.txt
Test1 10.10.1.5 HP LaserJet 4100 Series PCL Test Lab Test Printer
Test2 10.10.1.6 HP LaserJet 8150 Series PCL Engineering Rick's printer
Test3 10.10.1.7 HP Business Inkjet 2280 PCL 5C Operations Dave's printer
Using this sample file, Test1 and Test2 are created, Test3 is not because the driver is not in the Windows source.

The script dumps out four log files (one for each task) for each printer, e.g., PRINTER_NAME-1.log, PRINTER_NAME-2.log, PRINTER_NAME-3.log, PRINTER_NAME-4.log. So if you have a lot of printers in the input file, it will create a LOT of log files. Fortunately they are extremely small in size.

Sample Log Files

C:\Test1-1.log
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Created/updated port IP_10.10.1.5

C:\Test1-2.log
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Added printer driver HP LaserJet 4100 Series PCL

C:\Test1-3.log
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Added printer Test1

C:\Test1-4.log
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Configured printer Test1

If any task fails, the error(s) will be in the log file for that task. If any one step fails, all later tasks for that printer will also fail.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?