Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading registry via a batch file?

Status
Not open for further replies.

DigitalD

Technical User
Jul 15, 2003
6
US
I'm trying to find a decent way to keep track of which systems are staying up to date with their mcafee updates. What I would like to do is have a simple batch file that can be ran on each machine which scans a registry key and simply exports the key value back to a text file with some indicator of what machine it came from.

what I would like to see is something like this:

$computername$: 4.0.4307
$computername2$: 4.0.4304

the key that it needs to read is:
HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\VirusScan Engine\4.0.xx\szDatVersion

if someone could help me out with the reading the registry part or point me in the direction of a good tutorial on performing these kinds of operations (I didn't find anything good while googling for a solution)I would greatly appreciate it 0_o
 
OK, I did find something that would work on the mark vanderwoude page. his regread script looks like this once I customized the output:

@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Check command line
ECHO.%* | FIND "?" >NUL



:: Keep variables local
SETLOCAL

:: Read variables from command line
SET RegSection="HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\VirusScan Engine\4.0.xx"
SET RegKey="szDatVersion"
SET RegVal=


:: Store content of registry section in temporary file
REGEDIT /E "%Temp%.\readreg.dat" "%~1"

:: Use EGREP to search requested value in temporary file
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\readreg.dat" 2^>NUL ^| EGREP -i "^^\"?%~2\"?="') DO SET RegVal=%%B

:: Format and display the result
IF DEFINED RegVal (
SET RegVal=%RegVal:\\=\%
)
IF DEFINED RegVal (
ECHO.
ECHO %RegKey%=%RegVal%
ECHO %COMPUTERNAME%,%DATE%,%RegKey%=%RegVal% >> \\ntserver\admin\7-11\MATT\version.txt
)

:: Delete temporary file
IF EXIST "%Temp%.\readreg.dat" DEL "%Temp%.\readreg.dat"

:: Done
ENDLOCAL & SET RegVal=%RegVal%
GOTO:EOF


:Syntax
ECHO.
ECHO ReadReg.bat, Version 1.02 for Windows NT 4 / 2000 / XP
ECHO Read a value from the registry
ECHO.
ECHO Usage: READREG "section" "key"
ECHO.
ECHO Where: "section" is the section name, without brackets
ECHO "key" is the key whose value must be read
ECHO.
ECHO This batch file uses EGREP to search for the value
ECHO.
ECHO Example:
ECHO READREG "HKEY_CURRENT_USER\Environment" "path"
ECHO should return the user part of the PATH variable
ECHO.
ECHO Written by Rob van der Woude
ECHO

the only problem is that it doesn't seem to be set for the keys I entered for RegSection and RegKey, it still wants to pull variables from the command line. The variables will never change so I wanted to keep them permanent. But now it doesn't produce any output. Any ideas on where to go from here?
 
DOH! Missed a few variables that were no longer set. Final code as follows:

@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Check command line
ECHO.%* | FIND "?" >NUL



:: Keep variables local
SETLOCAL

:: Read variables from command line
SET RegSection="HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\VirusScan Engine\4.0.xx"
SET RegKey="szDatVersion"
SET RegVal=


:: Store content of registry section in temporary file
REGEDIT /E "%Temp%.\readreg.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\VirusScan Engine\4.0.xx"

:: Use EGREP to search requested value in temporary file
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\readreg.dat" 2^>NUL ^| EGREP -i "^^\"szDatVersion\"?="') DO SET RegVal=%%B

:: Format and display the result
IF DEFINED RegVal (
SET RegVal=%RegVal:\\=\%
)
IF DEFINED RegVal (
ECHO.
ECHO %RegKey%=%RegVal%
ECHO %COMPUTERNAME%,%DATE%,%RegKey%=%RegVal% >> \\ntserver\admin\7-11\MATT\version.txt
)

:: Delete temporary file
IF EXIST "%Temp%.\readreg.dat" DEL "%Temp%.\readreg.dat"

:: Done
ENDLOCAL & SET RegVal=%RegVal%
GOTO:EOF


:Syntax
ECHO.
ECHO ReadReg.bat, Version 1.02 for Windows NT 4 / 2000 / XP
ECHO Read a value from the registry
ECHO.
ECHO Usage: READREG "section" "key"
ECHO.
ECHO Where: "section" is the section name, without brackets
ECHO "key" is the key whose value must be read
ECHO.
ECHO This batch file uses EGREP to search for the value
ECHO.
ECHO Example:
ECHO READREG "HKEY_CURRENT_USER\Environment" "path"
ECHO should return the user part of the PATH variable
ECHO.
ECHO Written by Rob van der Woude
ECHO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top