Solved

Check the data of Registry key in batch script

Posted on 2011-03-15
5
1,081 Views
Last Modified: 2012-06-27
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\Windows\CurrentVersion\WindowsUpdate" /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.
0
Comment
Question by:agbnielsen
5 Comments
 
LVL 14

Accepted Solution

by:
sentner earned 500 total points
ID: 35144585
Your problem is in your errorlevel check.  the "IF ERRORLEVEL <N>" check will look for errorlevels of N or higher, so checking for errorlevel 0 checks for ALL errorlevels.

Just change the second line to:

IF ERRORLEVEL 1 GOTO :FAIL
0
 
LVL 14

Expert Comment

by:sentner
ID: 35144588
Some more details on that particular check:  http://support.microsoft.com/kb/69576
0
 
LVL 3

Expert Comment

by:OrenRozen
ID: 35144614
try this in the script

@echo off

for /F "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate" /v SusClientId') DO (
      if "%%a"=="TheScript" (echo yes) else (echo no)
)
0
 

Author Closing Comment

by:agbnielsen
ID: 35144619
thanks
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35145053
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\Windows\CurrentVersion\WindowsUpdate" /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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 77
Adding Namespace to the second node of the xml 16 62
C# Error - Add Failed 12 78
Filename to be appended with DateTimeof Download 3 43
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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

17 Experts available now in Live!

Get 1:1 Help Now