Solved

Need a script that will query a registry value on all of my windows servers

Posted on 2009-04-14
30
433 Views
Last Modified: 2012-05-06
I need a script that will query this registry path...
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing
value is
EnableConsoleTracing

for all  WIndows 2000/2003 Servers.  Does anyone have a batch or a vbs script that will query all servers in a input txt file?
0
Comment
Question by:mystikal1000
  • 10
  • 9
  • 5
  • +3
30 Comments
 

Expert Comment

by:jackmac4
ID: 24140598
I've always found when doing these types of tasks its easier to create a script using AutoITScript (http://www.autoitscript.com), compile it into an EXE and either launch it manually or write a script to run the compiled EXE rather than query the registry yourself.

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24140704
I can't do this install in the work environment.  Thanks anyways Jack.  I just need a dos batch or vbs script, I am sure someone has a similar script.
0
 
LVL 6

Expert Comment

by:Kentrix70
ID: 24141905
You can use the reg.exe tool that is included in Windows 2003 server.

See this link

http://www.petri.co.il/reg_command_in_windows_xp.htm

The syntax should be the same, though the examples are from a
Windows XP.

I don't think it is necessary to install the tool on the Windows 2000 servers,
to query them, but I have never tried that scenario. Always just used it
on Windows 2003 servers.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24141941
Please try:


@echo off

set RegPath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing
set RegKey=EnableConsoleTracing

for /f "skip=4 tokens=1,2*" %%a in ('reg query %RegPath% /v %RegKey%') do (
   set RegValue=%%c
)

echo %RegValue%
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24142303
Please find a script below that will do what you're asking. Make sure you modify the constants in the beginning of the batch script to match the name of the file you've got the server list in, and also the name of the output log file.

Justin Chandler
@echo off

:: Set the input file name and output file name here

set fil_serverListFile=SERVERS.TXT

set fil_outputLog=RESULTS.LOG
 
 

set fil_tempFile=%temp%\~%random%.tmp

if exist "%fil_outputLog%" del /f /q "%fil_outputLog%"

for /f %%A in ("%fil_serverListFile%") do set var_curServer=%%A&call :proc_queryServer

goto :eof
 

:proc_queryServer

reg query \\%var_curServer%\HKLM\SOFTWARE\Microsoft\Tracing /v EnableConsoleTracing > %fil_tempFile%

for /f "skip=4 tokens=1-2,*" %%A in (%fil_tempFile%) do set var_queryResults=%%C

echo %var_curServer%          %var_queryResults% >> %fil_outputLog%

goto :eof

Open in new window

24321396-Query-registry-value.cm.txt
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24142317
T010 - how do I check for all servers in a txt file?
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24142525
Please see my post above for what you're looking for.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24142784
I hope the following works for you. I've assumed your list of servers is in SERVERS.TXT.  You may change this to suit your own filenames.


@echo off

set ServerNames=servers.txt
set ResultsFile=results.txt

set RegPath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing
set RegKey=EnableConsoleTracing

(for /f %%s in (%ServerNames%) do (
   for /f "skip=4 tokens=1,2*" %%v in ('reg query \\%%s\%RegPath% /v %RegKey%') do (
      echo %%s %%v
   )
))>%ResultsFile%
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24143984
t0t0 - Its not working correctly, its not checking the servers.txt file when I run the file.
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 350 total points
ID: 24145602
Please try the following code.

The batch file also writes the date and time to the results file but this can be edited out if you don't want it to. Please let us know if this works and how you want data written to the results file for a final edit.


@echo off

set ServerNames=servers.txt
set ResultsFile=results.txt

set RegPath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing
set RegKey=EnableConsoleTracing

(if not exist %ServerNames% (
   echo %date% %time% %~nx0: Cannot find server list: %ServerNames%
   exit /b 1
))>>%ResultsFile%

(for /f "tokens=*" %%a in (%ServerNames%) do (
   echo.
   echo %%a
   for /f "skip=4 tokens=1,2*" %%b in ('reg query "\\%%a\%RegPath%" /v %RegKey%') do (
      echo.
      echo %date% %time% %%a %%d
   )
))>>%ResultsFile% 2>&1

exit /b 0
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24151042
t0t0 - this is strange I get this output with the new script...

Server1
The process cannot access the file because it is being used by another process.

Server2
The process cannot access the file because it is being used by another process.
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24152851
Justin - I tried your script and the batch runs for a sec and blinks off, but the Results.log file doesn't get created.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24155793
Good morning mystikal1000. I was away for several hours after posting my last comment.

I see this question is still unresolved. Not to worry. I'm sure you'll be on your way soon.

Thank you for your feedback. I must confess the error messages you're receiving are strange however, I'd like you to know I am currently looking into it.

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24157703
Thanks T0t0, please let me know if you need any additional information or diagnostics.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Assisted Solution

by:Justin_W_Chandler
Justin_W_Chandler earned 50 total points
ID: 24158682
Now that you've replied to me, I can correct the VERY SIMPLE mistake. As you can see by the working code I posted below, I just had to remove the quotes from the FOR command. It works now.

Justin Chandler
@echo off

:: Set the input file name and output file name here

set fil_serverListFile=SERVERS.TXT

set fil_outputLog=RESULTS.LOG
 
 

set fil_tempFile=%temp%\~%random%.tmp

if exist "%fil_outputLog%" del /f /q "%fil_outputLog%"

for /f %%A in (%fil_serverListFile%) do set var_curServer=%%A&call :proc_queryServer

goto :eof
 

:proc_queryServer

reg query \\%var_curServer%\HKLM\SOFTWARE\Microsoft\Tracing /v EnableConsoleTracing > %fil_tempFile%

for /f "skip=4 tokens=1-2,*" %%A in (%fil_tempFile%) do set var_queryResults=%%C

echo %var_curServer%          %var_queryResults% >> %fil_outputLog%

goto :eof

Open in new window

24321396-Query-registry-value.cm.txt
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 100 total points
ID: 24158873
mystikal1000 & t0t0:

t0t0's code worked on my workstation just fine. I think there may be a problem with the for "skip". Maybe a temporary file location is not writable or something else.  Let's try changing the code and see if we get the same error.
@echo off
 

set ServerNames=servers.txt

set ResultsFile=results.txt
 

set RegPath=HKLM\SOFTWARE\Microsoft\Tracing

set RegKey=EnableConsoleTracing
 

(if not exist %ServerNames% (

   echo %date% %time% %~nx0: Cannot find server list: %ServerNames%

   exit /b 1

))>>%ResultsFile%
 

(for /f "tokens=*" %%a in (%ServerNames%) do (

   echo.

   echo %%a

   for /f "tokens=1,2*" %%b in ('reg query "\\%%a\%RegPath%" /v %RegKey% ^| FIND /i "%RegKey%"') do (

      echo.

      echo %date% %time% %%a %%d

   )

))>>%ResultsFile% 2>&1
 

exit /b 0

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24158984
mystikal1000 & Justin_W_Chandler:

Justin_W_Chandler your line 9 needs to either have the quotes removed or add usebackq in the options in the for. As it stands your not getting the server names from servers.txt but using servers.txt as a server name.

Try changing line 9 with one of these.


for /f "usebackq" %%A in ("%fil_serverListFile%") do set var_curServer=%%A&call :proc_queryServer
 

for /f %%A in (%fil_serverListFile%) do set var_curServer=%%A&call :proc_queryServer
 

for /f %%A in ('type "%fil_serverListFile%"') do set var_curServer=%%A&call :proc_queryServer

Open in new window

0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24159114
AmazingTech,

I had already corrected my code. I hadn't any clue that there was even an issue until 5 minutes before I corrected it, as the author hadn't an opportunity to comment. Thanks for the input, regardless. I hadn't run it on my workstation as a test -- as it took me all of 5 minutes to create the extremely simple solution.

Justin Chandler
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24160113
Justin - I ran your script and it returned only the server names in the servers.txt file, not actual results.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24160389
Justin's results are in RESULTS.LOG not servers.txt this is the input file.
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24160488
The results.log returned server names only, not actual results.
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24161537
It does, in fact, log the results, as evidenced in this screenshot.
ss.JPG
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24165922
AmazingTech
Thank you for responding and for joining this thread although I apologise for being away most of the day and therefore, I have not been able to respond.

mystikal1000
AmazingTech has joined us. He is the highest ranking expert in this subject.

I'm still at a loss regarding the error messages you're receiving. The code works well in my environment and this has also been confirmed by AmazingTech.

I can only assume there is a problem accessing the registry files on your servers. As far as I/O is concerned, it appears you're getting the server names from servers.txt. Are the error messages written to results.txt or are they written to the screen? If they are written to results.txt then we can assume there are no issues with either the input file nor the output file. This leaves just the registry files.

Is it possible there is an access issue to your registry files via remote query?...


 
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24166024
mystikal1000

Just out of curiosity, are you trying this with just two servernames in servers.txt?

What results do you get if you edit your server.txt file so that it contains only two non-existent servers? - By doing this, we can narrow the errors right down to just the registry files if we get the following results in results.txt.

   SERVERS.TXT
   server_ABC
   server_XYZ

   RESULTS.TXT
   server_ABC
   Error: The network path was not found.
   server_XYZ
   Error: The network path was not found.

(I have not included blank lines)






0
 
LVL 16

Expert Comment

by:t0t0
ID: 24166038
mystikal1000

I forgot to add, the above test will confirm you are able to read from the servers.txt file and write to the results.txt file.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24166108
mystikal1000

After you have performed the above test, add a good servername to your servers.txt file and run the test again. I am expecting you will receive the same TWO results for the previous two non-existent servernames and just ONE error message (The process cannot access the file because it is being used by another process) for the good servername.

Please confirm the results.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24166119
You can also try typing the REG QUERY command directly on a command line to see what happens. Use a good servername, ie:

   REG QUERY \\server1\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing /v EnableConsoleTracing


Entering the following line should confirm you are able to query the local registry file:

   REG QUERY \\%username%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing /v EnableConsoleTracing


Entering a bad servername should result in "Error: The network path was not found", ie:

   REG QUERY \\server_ABC\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Tracing /v EnableConsoleTracing

Please confirm the results.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24172823
mystikal1000
I apologise for my absence. I have tried to help yo as best as I can. I really do hope you are able to resolve the issue regarding the error messages you are receiving. I am happy to leave this in your hands until I hear from you again.

Good luck and best wishes.

AmazingTech
Thank you for your help.
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 24204270
Thanks to all for giving me the solution to my issue.  All batch jobs work fine now...Ran it on a different computer...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now