Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

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

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?
Avatar of jackmac4
jackmac4
Flag of United States of America image

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.

Avatar of mystikal1000
mystikal1000

ASKER

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.
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.
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%
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
T010 - how do I check for all servers in a txt file?
Please see my post above for what you're looking for.
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%
t0t0 - Its not working correctly, its not checking the servers.txt file when I run the file.
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland 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
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.
Justin - I tried your script and the batch runs for a sec and blinks off, but the Results.log file doesn't get created.
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.

Thanks T0t0, please let me know if you need any additional information or diagnostics.
SOLUTION
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
SOLUTION
Avatar of AmazingTech
AmazingTech

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

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
Justin - I ran your script and it returned only the server names in the servers.txt file, not actual results.
Justin's results are in RESULTS.LOG not servers.txt this is the input file.
The results.log returned server names only, not actual results.
It does, in fact, log the results, as evidenced in this screenshot.
ss.JPG
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?...


 
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)






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.
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.
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.
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.
Thanks to all for giving me the solution to my issue.  All batch jobs work fine now...Ran it on a different computer...