Batch file to grab all local printers.

I am trying to pull some information of all printers off  each machine in my environment. I wrote a batch file but I am having some problems with it, I am not a script wiz so I could use some help. Have a look at the code and maybe you can tell me what I did wrong.

@echo off

setlocal enabledelayedexpansion

set printers_key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers
set name_val=Name
set port_val=Port
set location_val=Location
set description_val=Description
set printerdriver_val=Printer Driver
set sharename_val=Share Name
set temp_key=
set temp_name=

for /f "tokens=7 delims=\" %%a in ('reg.exe query "%printers_key%"') do (
set temp_key=%printers_key%\%%a
for /f "tokens=7 delims=\" %%i in ('reg.exe query "%temp_key%"') do (
  for /f "tokens=2*" %%a in ('reg.exe query "%temp_key%" /v "%name_val%" 2^>NUL ^| find /i "%name_val%"') do @echo Printer Name = %%~b >> "%userprofile%\desktop\Printers.txt"
  for /f "tokens=3*" %%a in ('reg.exe query "%temp_key%" /v "%printerdriver_val%" 2^>NUL ^| find /i "%printerdriver_val%"') do @echo Model = %%b >> "%userprofile%\desktop\Printers.txt"
  for /f "tokens=2*" %%a in ('reg.exe query "%temp_key%" /v "%port_val%" 2^>NUL ^| find /i "%port_val%"') do @echo Port = %%~b >> "%userprofile%\desktop\Printers.txt"
  for /f "tokens=2*" %%a in ('reg.exe query "%temp_key%" /v "%location_val%" 2^>NUL ^| find /i "%location_val%"') do @echo Location = %%~b >> "%userprofile%\desktop\Printers.txt"
  for /f "tokens=2*" %%a in ('reg.exe query "%temp_key%" /v "%description_val%" 2^>NUL ^| find /i "%description_val%"') do @echo Description = %%~b >> "%userprofile%\desktop\Printers.txt"
  for /f "tokens=3*" %%a in ('reg.exe query "%temp_key%" /v "%sharename_val%" 2^>NUL ^| find /i "%sharename_val%"') do @echo Share Name = %%~b >> "%userprofile%\desktop\Printers.txt"
)
)
pause
exit​

Open in new window


In the first for loop I can't get it to set the temp_key variable, but I can put that command right after the "do" without a "("  it works but only for the last printer it finds and it ignores the others. I am stuck and just can't seem to get anywhere with it. Any help it much appreciated.

Thanks
Ryan BennettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan BennettAuthor Commented:
Maybe I'm making it way to complicated and then getting lost in the logic. Or maybe I'm having a low blood sugar day, either way it's not working.

Thanks for the help
0
NVITCommented:
Try this. For all %temp_key% vars in the FOR loop after line 16, i.e. set temp_key=%printers_key%\%%a
...change to... !temp_key!

It looks like they are all reg.exe query "%temp_key%"
0
oBdACommented:
You can do that with a WMI query.
@echo off
setlocal
set LogFile=%UserProfile%\Desktop\Printers.txt
set PropertyList=Name, Description, Location, DriverName, PortName, ShareName
wmic.exe Printer WHERE Local=True GET %PropertyList% /format:list >"%LogFile%"

Open in new window

A full list of properties is here:
Win32_Printer class
https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Benjamin VoglarIT ProCommented:
In powershell you can do this with 1 line:

Get-WmiObject -Class win32_printer -ComputerName remote_comp | select  PSComputerName,Name,DriverName,PortName,Location,Description,ShareName | Export-Csv C:\exports\printers1.csv

Open in new window

0
Bill PrewCommented:
In terms of your original code, if you wanted to get that working here are some corrections / suggestions.

But I would take a good look at WMIC as mentioned above if you want to do this from a BAT script, that can make things a lot simpler.

@echo off

setlocal enabledelayedexpansion

set printers_key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers
set name_val=Name
set port_val=Port
set location_val=Location
set description_val=Description
set printerdriver_val=Printer Driver
set sharename_val=Share Name
set report=%userprofile%\desktop\Printers.txt

(
  for /f "tokens=7 delims=\" %%P in ('reg.exe query "%printers_key%"') do (
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%name_val%" 2^>NUL ^| find /i "%name_val%"') do echo Printer Name = %%~b
    for /f "tokens=3*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%printerdriver_val%" 2^>NUL ^| find /i "%printerdriver_val%"') do echo Model = %%b
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%port_val%" 2^>NUL ^| find /i "%port_val%"') do echo Port = %%~b
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%location_val%" 2^>NUL ^| find /i "%location_val%"') do echo Location = %%~b
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%description_val%" 2^>NUL ^| find /i "%description_val%"') do echo Description = %%~b
    for /f "tokens=3*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%sharename_val%" 2^>NUL ^| find /i "%sharename_val%"') do echo Share Name = %%~b
  )
)>"%report%"

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan BennettAuthor Commented:
Bill that's awesome, I figured I was stepping on my own toes. Never thought of using the log file that way either, nicely done thanks tons.

Benjamin, the reason I needed a batch file is because this is just part of another batch file I am using. thanks though.

oBdA, I had started out playing with the wmic commands but just could not get to format nicely enough to be very readable. thanks much.

NVIT, I tried your suggestion, it got me closer, but I ended up with 6 sets of each printer on the machine instead of one. Thanks for the help.


 I really appreciate the extremely fast advice and help !


Thanks,
Ryan
0
Bill PrewCommented:
Glad that was helpful.

With regard to WMIC, it seems to me that the output of the exampled oBdA posted was nearly the same as your scripts output, just much faster.  Curious what it was about that output that you would want changed, there may be ways to accomplish that.

~bp
0
Ryan BennettAuthor Commented:
The wmic out put seems to mess with the out put file coding or something, I am pushing this out to machines through ZENworks and it seems to scramble it when using wmic commands and put extra carriage returns and so on. I just tried oBdA's code with me main script and the log file was completely scrambled with strange symbols, but running it by itself works fine. (????) But for this project your code is what I needed.
0
Ryan BennettAuthor Commented:
While I am at it, I will post another question to be fair, but just to get you thinking about it, I am wondering if there is a way to add to this code where if a port of a printer is found we could get it's MAC address and pop it in there with the printer information as well?

 Like say if portname contains xxx.xxx.xxx.xxx (or something) then send the port name information to a nbtstat -a %portname% command and then grab the "MAC address = xx-xx-xx-xx-xx-xx" portion of that out put  and pop it in with the out put of each printer?

BTW, whats the best way to post a new question and refer back to this one? paste a URL?


Thanks.
0
Ryan BennettAuthor Commented:
Sorry I meant to say if the port of a printer is found to be an IP address then process it to get the MAC address....
0
Bill PrewCommented:
BTW, whats the best way to post a new question and refer back to this one? paste a URL?
Yes, that will certainly work.  Honestly, there used to be a feature in the question section that support linking related questions, but I can't seem to see a way to do it currently.

I will ask some of the moderators if it still exists, but for now you can just include the url of one question in the other.  I would suggest when you add the new question include a link to this one in the question description.  And then when you get a new question, grab it's URL and add a comment here with that as a follow on question.

Apologies in advance if I am just being dumb and missing an obvious way on the site to do this "automatically".

~bp
0
Bill PrewCommented:
As far as WMIC output goes, you are right, it can mess with EOL characters, a known "feature".

One way to work around that I think is to pipe the output to the TYPE command before processing, so something like this, although that can introduce extra blank lines as I recall...

wmic.exe Printer WHERE Local=True GET Name, Description, Location, DriverName, PortName, ShareName /format:list|more>wmiclog.txt

Open in new window

~bp
0
Ryan BennettAuthor Commented:
I will try that out on the other commands that are messing up the formatting in the rest of the out put, thanks.

BTW I posted the other question here if you wanna take a look at it and see if it's doable or not. I would appreciate it very much.

Thanks again.
0
Ryan BennettAuthor Commented:
That does work good to format it correctly, other than the extra blank line before and after the out put. Other than that it's a lot better to look at and more readable then the default wmic out put.

 Thanks.
0
Bill PrewCommented:
Great.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.