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

ryder0707
ryder0707 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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
Top Expert 2009

Author

Commented:
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

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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)
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Top Expert 2009

Author

Commented:
Yes that the idea...how to grab the info without asking user to login? Anyone?
Must be stored somewhere in registry
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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.
Top Expert 2009

Author

Commented:
Nope you are wrong it is stored locally in the registry, see my other Q http://www.experts-exchange.com/Hardware/Peripherals/Printers_Scanners/Q_26306757.html
Other expert?
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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?

Lead SaaS Infrastructure Engineer
Commented:
So if you're confident the printers are not being un-mapped by a process on the computer we can definitely find them in the registry.

I'm not as good at VB Script as I used to be; however here is a Batch file to do what you are looking for.  Certainly a little easier to understand anyway.

Hope that resolves your issues.



::------------------------------------------------------------------------------------------------::
:: Script: FindRemotePrinter.Bat                                            				 	  ::
:: Version: 1.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-07-06                                                                               ::
::                                                                                                ::
::                                                                                                ::
:: Desc: This script Mounts a Registry Hive on a machine and parses the names of the printers	  ::
::																								  ::
::------------------------------------------------------------------------------------------------::
:Begin
@ECHO OFF

::------------------------------------------------------------------------------------------------::
:: This section sets up default variable settings and sets the logging directory and file name.   ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
	CALL :Start-Set-Date
	GOTO :End-Set-Date
	:Start-Set-Date
		SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
		SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
		GOTO :EOF
	:End-Set-Date
	SET Preamble=FindRemotePrinter --
	SET SrcUser=USERNAME_To_Find_Printers_Of
	SET SrcComputer=REMOTE_COMPUTERNAME_NO_\\s
	SET SrcShare=C$\Documents And Settings
	SET PLog=%SrcComputer%_%SrcUser%_%TDATE%_%TTIME%.log
	SET IDir=c:\FindRemotePrinter\Log
	SET ILog=ILog_%TDate%_%TTime%.Log
	:Start-IDir
		ECHO %Preamble% Checking For %IDir%
		IF EXIST %IDir% ECHO %Preamble% IDir "%IDir%" -- Found! -- GOTO End-Prep >> %IDir%\%ILog%
		IF EXIST %IDir% GOTO End-IDir
		ECHO %Preamble% Creating %IDir%
		mkdir %IDir%
	:End-IDir

:End-Prep

:Start-Main-Processing

	:Start-PrintLog-Header
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO -- Computer: %SrcComputer%					-- >> %IDir%\%PLog%
	ECHO -- User: %SrcUser%							-- >> %IDir%\%PLog%
	ECHO -- Date: %TDate%							-- >> %IDir%\%PLog%
	ECHO -- Time: %TTime%							-- >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO. >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO -- The Following Printers Were Discovered:	-- >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	:End-Printlog-Header


	:Start-Open-Reg-File
		reg LOAD HKEY_USERS\%SrcUser% "\\%SrcComputer%\%SrcShare%\%SrcUser%\NTUSER.DAT"
	:End-Open-Reg-File

	:Start-Find-Printer
		ECHO %Preamble% Start-Find-Printer --
		ECHO %Preamble% Start-Find-Printer -- >> %IDir%\%ILog%
		for /F %%A IN ('reg query HKEY_USERS\%SrcUser%\Printers\ /s') DO CALL :Start-Print-Loop %%A
		GOTO End-Find-Printer
		:Start-Print-Loop
			SET PTemp=%1
			ECHO %PTemp% | FIND /I "HKEY_USERS"
			IF ERRORLEVEL 1 GOTO Start-IS-Printer
			GOTO END-IS-Printer
			:Start-Check-Default
				ECHO %PTemp% | FIND /I "Default"
				IF ERRORLEVEL 1 GOTO Start-IS-Printer
				GOTO END-IS-Printer
			:End-Check-Default

			:Start-IS-Printer
				ECHO %Preamble% The following Printer was Found - %PTemp% -- And Stored in %IDir%\%PLog% --
				ECHO %PTemp% >> %IDir%\%PLog%
				ECHO %Preamble% The following Printer was Found - %PTemp% -- And Stored in %IDir%\%PLog% -- >> %IDir%\%ILog%
			:END-IS-Printer
			GOTO :EOF
		:End-Print-Loop	
	
	:End-Find-Printer
	ECHO %Preamble% The Registry has no more entries at %LookupKey% --
	ECHO %Preamble% The Registry has no more entries at %LookupKey% -- >> %IDir%\%ILog%	

	:Start-Close-Reg-File
		reg UNLOAD HKEY_USERS\%SrcUser%
	:End-Close-Reg-File
	
:End-Main-Processing


:End
	CALL :Start-Set-Date
	ECHO %Preamble% The Batch File Completed ON %TDATE% AT %TTIME% --
	ECHO %Preamble% The Batch File Completed ON %TDATE% AT %TTIME% -- >> %IDir%\%ILog%	

Open in new window

Commented:
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.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
@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.

Commented:
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...
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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.

=)
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
::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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial