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 = fspenTextFile("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"
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.