Link to home
Start Free TrialLog in
Avatar of ryder0707
ryder0707Flag for Malaysia

asked on

Need vbs script to get list of printers based on user profile

Need vbs script to get list of printers based on user profile stored on a PC
I will need to run this script agains remote PCs
Please help
Avatar of senthil_var2000
senthil_var2000

can u give me the command u r trying to run in command prompt also the required out so that i can generate the vbs file
Avatar of ryder0707

ASKER

I tried to use the code below, but I'm not getting any network printers so I'm thinking might be because the script is querying based on my user id/profile, the user that is using the PC will have many printers available/mapped when he login to the PC
'=========================================================================='  
' Title:   List Printers.vbs'  
' Date:    02/23/2010'  
' Author:  Bradley Buskey'  
' Version: 1.00'  ' Updated: 02/23/2010'  
' Purpose: List all printers attached to a workstation'  
'=========================================================================='  
Const ForAppending = 8  
Const ForReading = 1   
strComputer = inputbox("Please enter the computer name or IP address.","Computer Name",".")   
Set WshNetwork = CreateObject("WScript.Network")  
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")  
Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")  
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)  
Set WshShell = WScript.CreateObject("WScript.Shell")  
Set objFSO = CreateObject("Scripting.FileSystemObject")   
For Each objItem in colItems      
UserName = objItem.UserName      
arrUserName = Split(UserName, "\", -1, 1)      
varUserName = arrUserName(1)  
Next   
filOutput = varUserName & ".txt"   
If objFSO.FileExists(filOutput) Then      
objFSO.DeleteFile(filOutput)  
End If   
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)  
For Each objPrinter in colInstalledPrinters      
strTest = Left(objPrinter.Name, 2)      
objOutputFile.WriteLine(objPrinter.Name)  
Next   
objOutputFile.Close   
varOpen = MsgBox("Do you want to view the printers?",36,"View File?")  
If varOpen = vbYes Then
varCommand = "notepad " & filOutput      
WshShell.Run varCommand,1,False  
End If   
Wscript.Sleep 1500  
MsgBox "Printer mappings have been stored in '" & filOutput & "'.", 64, "Script Complete"  
Wscript.Quit

Open in new window

Avatar of Ben Personick (Previously QCubed)
if printers are being mapped on login, they are likely being mapped by a login script using the Con2Prnt.exe executable, or via GPO, those you will be best to look at the batch script or GPO to determine, as they may not be present except while the user is logged on (as generally you want to delete network printers at log off on a laptop etc)
Yes that the idea...how to grab the info without asking user to login? Anyone?
Must be stored somewhere in registry
Ryder.

  If you are using a logon script to map the printers you will need to look at the logon script.
  If printers are mapped by Group policy, you will need to review the GPO.

That information is not stored on the local computer, it is stored in the script and GPO itself.
Nope you are wrong it is stored locally in the registry, see my other Q https://www.experts-exchange.com/questions/26306757/List-installed-mapped-newtork-printers-in-a-PC-for-a-user-remotely.html
Other expert?
This Assumes the printers are not unmapped at log-off, have you confirmed this is the case?  If there is a log-off script which deletes the printers they will not appear in the registry any longer..

Which is  part of why it's easier to check the scripts to see what they are installing and un installing.

However in that thread you suggested doing the enumeration while the user is logged on, which should work, and in this thread you specifically want the information while the user is not logged on.

May I ask what you are trying to accomplish with this script?

I imagine you're trying to troubleshoot an issue where a user either has a printer they do not need or they do not have a printer they need, is this correct?

ASKER CERTIFIED SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There's a fundamental problem with getting information from user's profiles.  In order for you to search the registry on a remote PC, the user's profile must be loaded.  This happens normally when a person is logged in... but must be done via code if the PC is powered on without anyone logged in (or in the situations where there are multiple users of a PC and only one is logged in at a time).
Mounting remote registry hives, performing the search, and then unmounting the registry hives is not something that can be easily done via a script.   If you'e interested in a real application, let us know, as this can be done fairly easily in VB.Net/C#, etc.
@Graye If you will note I have mounted the remote registry hive for an offline user int he script above using standard Microsoft components included with any client XP or greater.  No special coding required.
QCubed... that's a cool solution where you mount the remote registry hive on the local PC.   I always did it the other way... I mounted the hive on the remote PC and then searched the remote registry...
Thanks!!  =)

It took a little testing to get it to work because I normally work with the online registry not the offline registry.

But I've tested it in my environment and it works just fine as long as you have Administrator permissions to the computer and the user exists.

=)
::Poke::

My batch script functions according to the specifications of the request:

It connects to a remote machine
It loads the registry of a user specified in the script who is not logged on
It finds all the printers of the user which were not unmapped
And additionally outputs them into a log file as well as posting them on the screen.

As I stated above if the printers are uninstalled by some process the settings will be removed from the registry, and therefore there will be no reliable way to finding them.