5

Active Directory: VBscript to enumerate all Active Directory published printers

Posted March 16th, 2010 in printers and tagged , , , , , , , , , , , by dirk adamsky

Topic of today is printing.
I have made a script that enumerates all Active Directory published printers.
By adding or removing attributes from arrAttributes you can change the output to your specific needs.

Follow the next steps to run the script (no admin rights needed):

* open your favorite text editor
* copy and paste the script into the editor
* save the script (for example c:\temp\adprinters.vbs)
* open a command prompt
* go to “c:\temp”
* give “cscript adprinters.vbs” (without quotes) and enter

The script:

' Name : adprinters.vbs
' Description : script to enumerate all Active Directory published printers
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 16-03-2010
' Level : intermediate

arrAttributes = Array("name","printerName","location","serverName") 

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
Set objRootDSE = Nothing

strFilter = "(objectClass=printQueue)"
strAttributes = Join(arrAttributes,",")
Wscript.Echo Join(arrAttributes,";")
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	On Error Resume Next
	strTempOutput = ""
	For i = 0 To Ubound(arrAttributes)
		strTempOutput =  strTempOutput & " ; " & adoRecordset.Fields(arrAttributes(i)).Value
		strOutput = Mid(Ltrim(strTempOutput),3)
	Next
	Wscript.Echo strOutput
	adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

Jérémie (see comment below) had a problem with adding the “description” attribute, the reason for the error is that the description attribute is of type “array” or “multi-value”.
I just made an updated version with the description attribute.

The updated script:

' Name : adprinters.vbs
' Description : script to enumerate all Active Directory published printers with description attribute
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 13-07-2011
' Level : intermediate

arrAttributes = Array("name","printerName","location","serverName") 

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
Set objRootDSE = Nothing

strFilter = "(objectClass=printQueue)"
strAttributes = Join(arrAttributes,",") & ",description"
Wscript.Echo strAttributes
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	On Error Resume Next
	strTempOutput = ""
	For i = 0 To Ubound(arrAttributes)
		strTempOutput =  strTempOutput & " ; " & adoRecordset.Fields(arrAttributes(i)).Value
		strOutput = Mid(Ltrim(strTempOutput),3)
	Next
	arrDescription = adoRecordset.Fields("description").Value
	For Each strDescription in arrDescription
		Wscript.Echo strOutput & " ; " & strDescription
	Next
	adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

When you have problems/questions please post a reply, you can also rate the script.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV


Related Posts:
  • Active Directory: VBscript to enumerate nested Active Directory groups from an Excel sheet
  • Active Directory: VBscript to enumerate the local profile size of all computers and users in Active Directory
  • VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file

  • 5 Responses so far.

    1. Mana says:

      Thanks for posting this script. I am thinking of creating a Intranet page which can help all the users in connecting to printers in the AD. I want to enumerate all the printers published in an OU, list them on a simple webpage with a functionality of right clicking an connecting to any of them depending upon the location. Could you please help?

    2. dirk adamsky says:

      Hi Mana,

      It’s possible to output to a html file.
      I did some research on connecting to a printer from a website:
      for what I see it was possible with older browser but in modern browsers it does not work anymore.
      Do you want me to extend the script with a log to html function?

      Best regards,

      dirk adamsky

    3. Jérémie says:

      Hi i’d looking for the attribute named “description” a can’t print it.

      In the description I have information who help to identify the printer.

      thank for your help

    4. dirk adamsky says:

      Hi Jérémie,

      Unformtunately the “description” attribute of a printqueue object is of type “array”. That is why adding the description attribute fails.
      I will make you an updated version of the script.

      Best regards,

      dirk adamsky

    5. dirk adamsky says:

      Hi Jérémie,

      I updated the article with a second script with the “description” attribute included. Hope this is what you want.

      Best regards,

      dirk adamsky

    Leave a Reply