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
