Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

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?
0
mystikal1000
Asked:
mystikal1000
  • 10
  • 9
  • 5
  • +3
3 Solutions
 
jackmac4Commented:
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
 
mystikal1000Author Commented:
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
 
Kentrix70Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
t0t0Commented:
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
 
Justin_W_ChandlerCommented:
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
 
mystikal1000Author Commented:
T010 - how do I check for all servers in a txt file?
0
 
Justin_W_ChandlerCommented:
Please see my post above for what you're looking for.
0
 
t0t0Commented:
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
 
mystikal1000Author Commented:
t0t0 - Its not working correctly, its not checking the servers.txt file when I run the file.
0
 
t0t0Commented:
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
 
mystikal1000Author Commented:
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
 
mystikal1000Author Commented:
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
 
t0t0Commented:
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
 
mystikal1000Author Commented:
Thanks T0t0, please let me know if you need any additional information or diagnostics.
0
 
Justin_W_ChandlerCommented:
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
 
AmazingTechCommented:
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
 
AmazingTechCommented:
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
 
Justin_W_ChandlerCommented:
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
 
mystikal1000Author Commented:
Justin - I ran your script and it returned only the server names in the servers.txt file, not actual results.
0
 
AmazingTechCommented:
Justin's results are in RESULTS.LOG not servers.txt this is the input file.
0
 
mystikal1000Author Commented:
The results.log returned server names only, not actual results.
0
 
Justin_W_ChandlerCommented:
It does, in fact, log the results, as evidenced in this screenshot.
ss.JPG
0
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
mystikal1000Author Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now