VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file

Posted May 27th, 2011 in printers by dirk adamsky

Today’s script is made for Hassan.
He asked me to do a script that enumerates the printers of each user. Although that sounds easy it is a little complicated.

The printers of each user are saved in the user profile
(in the ntuser.dat file to be precisely).
The easiest way to get a list of printers per user is to add a small script to the logon script (can also be done with a group policy).
The script below is such a script.
It is very compact and can be run under the users credentials.
The script does a WMI query (the Win32_Printer Class) for the printers and writes the output to a file.
Unfortunately we cannot use the same file for all users because of potential “file locking” problems.
This can be adressed by using a database but that is beyond the scope of this article.

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
* change the UNC path (“\srvXXXlogfiles”) in the LogToFile function on line 19 to your UNC path
* save the script (for example c:tempprinters.vbs)
* open a command prompt
* go to “c:\temp”
* give “cscript printers.vbs” (without quotes) and enter

To run the script as a logon script you can copy the script to the netlogon share or another user accessible location.

The script:

' Name : printers.vbs
' Description : script to enumerate the printers of Active Directory users and write the results to a file
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 27-05-2011

Set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\.rootcimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
    LogToFile(objNetwork.UserDomain & "" & objNetwork.UserName & " ; " & objPrinter.Name)
Next
Set colPrinters = Nothing
Set objWMIService = Nothing
Set objNetwork = Nothing 

Function LogToFile(Message)
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = ObjFSO.OpenTextFile("\srvXXXlogfiles" & objNetwork.UserName & Date & ".txt",8,True)
objTextFile.WriteLine Message
objTextFile.Close
Set objTextFile = Nothing
Set ObjFSO = Nothing
End Function

When you have problems/questions with the script please post a reply.

Happy scripting.

Best regards,

Dirk Adamsky

Active Directory: VBscript to enumerate all Active Directory published printers

Posted March 16th, 2010 in printers 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