agbnielsen
asked on
Check the data of Registry key in batch script
Hi,
I am writing a small batch script that looks to the registry on a client and checks to see if the data of a registry key matches a single string. The reason for this is because the key in question has been duplicated in many instances and I would like to call a whole heap of things in the batch script if the key matches the duplicated string. I have written the following to ensure that I have this working correctly:
reg query "HKLM\SOFTWARE\Microsoft\W indows\Cur rentVersio n\WindowsU pdate" /v SusClientId | FIND /I "Test_String"
IF NOT ERRORLEVEL 0 GOTO :FAIL
ECHO THIS KEY EXISTS!
GOTO :SUCCESS_1
:FAIL
ECHO THIS KEY DOES NOT EXIST!
:SUCCESS_1
PAUSE
Now "Test_String" is not the data of SusClientId, so I guess it should return an ErrorLevel of 1 (or something not 0) and echo "THIS KEY DOES NOT EXIST!". But in fact it doesn't, it echos THIS KEY EXISTS!. I guess I have something wrong with FIND?
Any help would be appreciated. Note that I am not a batch wizard.
I am writing a small batch script that looks to the registry on a client and checks to see if the data of a registry key matches a single string. The reason for this is because the key in question has been duplicated in many instances and I would like to call a whole heap of things in the batch script if the key matches the duplicated string. I have written the following to ensure that I have this working correctly:
reg query "HKLM\SOFTWARE\Microsoft\W
IF NOT ERRORLEVEL 0 GOTO :FAIL
ECHO THIS KEY EXISTS!
GOTO :SUCCESS_1
:FAIL
ECHO THIS KEY DOES NOT EXIST!
:SUCCESS_1
PAUSE
Now "Test_String" is not the data of SusClientId, so I guess it should return an ErrorLevel of 1 (or something not 0) and echo "THIS KEY DOES NOT EXIST!". But in fact it doesn't, it echos THIS KEY EXISTS!. I guess I have something wrong with FIND?
Any help would be appreciated. Note that I am not a batch wizard.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some more details on that particular check: http://support.microsoft.com/kb/69576
try this in the script
@echo off
for /F "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\W indows\Cur rentVersio n\WindowsU pdate" /v SusClientId') DO (
if "%%a"=="TheScript" (echo yes) else (echo no)
)
@echo off
for /F "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\W
if "%%a"=="TheScript" (echo yes) else (echo no)
)
ASKER
thanks
I know you have your answer, but for future you might also like to know of && and || as shortcuts to using if errorlevel:
&& runs a command if the previous command is success, || if it fails, so, e.g.
reg query "HKLM\SOFTWARE\Microsoft\W indows\Cur rentVersio n\WindowsU pdate" /v SusClientId | FIND /I "Test_String" && goto success
echo The key was not found
pause
goto :eof
:success
echo The key was found
pause
goto :eof
pause
&& runs a command if the previous command is success, || if it fails, so, e.g.
reg query "HKLM\SOFTWARE\Microsoft\W
echo The key was not found
pause
goto :eof
:success
echo The key was found
pause
goto :eof
pause