Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to Query PRINTERS through Active Directory

Status
Not open for further replies.

olorean

Programmer
Dec 5, 2002
13
0
0
US
I am trying to query ALL the printers in the Active Directory.

I can see all the users, but not the printers. In the GUI interface "Active Directory Users and Computers" I can see a list of ALL printers.

Any idea how this is done through a query?

 
We've just completed something recently regarding what your want to do. Here's some VBS code that will go out and get the all the printers in AD and print them to a file. The LDAP statement will need to be modified to reflect your AD structure.

Set oCN = GetObject("LDAP://OU=Printers,DC=mydomain,DC=com")
Set fso = CreateObject("Scripting.FileSystemObject")
Set NewFile = fso_OpenTextFile("C:\Printers.csv", 2, True)

For each oPrinter in oCN
'wscript.Echo oPrinter.printerName & oPrinter.location
NewFile.WriteLine oPrinter.prinsharename &","& oPrinter.printerName &","& oPrinter.location &","& oPrinter.description
Next
NewFile.Close
wscript.echo "Done"
 
I appologize, I am a relative Novice at many of these things.

I understand everything that you have listed, up to
oPrinter.prinsharename

I did not declare oPrinter anywhere, so I do not know how to capture the field information.

Should I be using a oCN.get("prinsharename") or do I have the wrong syntax?

I do appreciate the help. Thank you.
 
Oops, theres a typo there. It should read oCN.printsharename.

Anyways, your question makes me think you need an explanation of what's happening in the script:

Set oCN = GetObject("LDAP://OU=Printers,DC=mydomain,DC=com")
'Creates an object called oCN that has all of the items in the OU called "Printers" that are in the AD domain "mydomain.com"

The next two lines set up the creation of the file that will be written to. They're not AD related, but hey, you need to see the result somewhere, right?

For each oPrinter in oCN
NewFile.WriteLine oPrinter.printsharename &","& oPrinter.printerName &","& oPrinter.location &","& oPrinter.description
Next
'The FOR/NEXT loops through the object oCN and writes the child properties "printShareName" "printerName" "location" and "description".

The important thing to remember is that you're working with an OBJECT and that objects have properties. So you need to do some research and find out what properties are associated with the OU Printer object. An easy way is to use the Active Directory Browser that comes with the Active Directory Service Interface (ADSI) SDK. I can't find the link right now, but try searching at MS.

Hope this helps.

 
I just noticed I didn't really answer your question about what "oPrinter" is. The 'for each oPrinter in oCN' creates a new object called oPrinter that has the properties for each item in the object oCN. So oPrinter.printShareName references the property "printShareName" in one element of the object oCN. So "for each" element "in oCN" set oPrinter to become it. Not a great explanation, but if you give it shot, you'll see how it works.
 
Thank you very much.

This does help alot.

I am having a problem with the ou=Printers. I am getting the error "Table not found"

I will keep trying.

Your help and explination are greatly appreciated.

Christopher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top