How can I use a script to search for printers listed in Active Directory (AD).

John Savill

November 6, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. The following script, which you can download here takes as an argument the name of a domain to search (e.g., dc=savilltech,dc=com), then lists the printer queues and the server the printer is running on.

Const ADS_SCOPE_SUBTREE = 2' Check that all required arguments have been passed.If Wscript.Arguments.Count  required. For example:" & vbCrLf _        & "cscript searchprinters.vbs dc=savilltech,dc=com"    Wscript.Quit(0)End IfstrRootSearch = Wscript.Arguments(0)Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnectionobjCommand.CommandText = "Select printerName, serverName from " _    & " 'LDAP://" & strRootSearch & "' where objectClass='printQueue'"objCommand.Properties("Page Size") = 1000objCommand.Properties("Timeout") = 30objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREEobjCommand.Properties("Cache Results") = FalseSet objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo Until objRecordSet.EOF    Wscript.Echo "Printer Name: " & objRecordSet.Fields("printerName").Value    Wscript.Echo "Server Name: " & objRecordSet.Fields("serverName").Value    objRecordSet.MoveNextLoop

Here's a sample execution of the script:

C:>cscript searchprinters.vbs dc=savilltech,dc=comPrinter Name: Phaser860Server Name: DALSDC01.savilltech.comPrinter Name: CWISServer Name: DALSDC01.savilltech.comPrinter Name: ProjectServer Name: DALSDC01.savilltech.comPrinter Name: HRServer Name: DALSDC01.savilltech.comPrinter Name: HP LaserJet 4M PlusServer Name: DALSDC01.savilltech.comPrinter Name: ProjectServer Name: dalsxc01.savilltech.com

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like