Link to home
Start Free TrialLog in
Avatar of Medquest
MedquestFlag for United States of America

asked on

reg query and if exist for a value

guys,

I am just trying to query the registry for a specific value and if it finds that value I want to the batch file to exit and if not to run the next line.

I start working with this one, but right now it's not reacting to the if statement:

*START*
@echo off
reg query HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ
if exist winmgmt goto Done
net stop MOM
Echo Y | reg add HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
net start MOM
:Done
Exit
*END*

any help will be greatly appreciate it.
Avatar of Medquest
Medquest
Flag of United States of America image

ASKER

sorry forgot to add this image.
Registry-Image.jpg
Avatar of Bill Prew
Bill Prew

Try this.

~bp
@echo off
setlocal
set "DOS="
for /f "tokens=3" %A in ('reg query HKLM\SYSTEM\CurrentControlSet\services\mom /v DependOnService /t REG_MULTI_SZ 2^>NUL') do set "DOS=%A"
if not "%DOS%"=="" (
  net stop MOM
  Echo Y | reg add HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
  net start MOM
)

Open in new window

billprew,
Thanks for your respose. I tride the code you sent me and got this error message:
A"  was unexpected at this time.
Sorry, I was testing the core command at a command line, and the syntax is slightly different when used in a batch file.  Try this.

~bp
@echo off
setlocal
set "DOS="
for /f "tokens=3" %%A in ('reg query HKLM\SYSTEM\CurrentControlSet\services\mom /v DependOnService /t REG_MULTI_SZ 2^>NUL') do set "DOS=%%A"
if not "%DOS%"=="" (
  net stop MOM
  Echo Y | reg add HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
  net start MOM
)

Open in new window

this one is a little better, but it runs regardless of the existence of the value. how can I make it exit if the value already exists?
Okay, let's try a slight different approach.

~bp
@echo off
reg query HKLM\SYSTEM\CurrentControlSet\services\mom /v DependOnService /t REG_MULTI_SZ >NUL 2>&1
if ERRORLEVEL 1 (
  net stop MOM
  reg add HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog /f
  net start MOM
)

Open in new window

now it's not doing anything no more.
That's odd, I did a test here and it was working fine, only executing the logic when the registry value was not found.  Can you try this little test batch file and let me know what it displays?

~bp
@echo off
reg query HKLM\SYSTEM\CurrentControlSet\services\mom /v DependOnService /t REG_MULTI_SZ >NUL 2>&1
if ERRORLEVEL 1 (
  ECHO Not found 1
)
reg query HKLM\SYSTEM\CurrentControlSet\services\mom /v XDependOnService /t REG_MULTI_SZ >NUL 2>&1
if ERRORLEVEL 1 (
  ECHO Not found 2
)

Open in new window

that's what I got:
Not found 2
Oky, that is the correct results.  So the prior script should be doing what you want.  Are you sure you ran it on a system where the key didn't already exist (I have to ask).

Try adding the following line right before the "net stop mom" line:

ECHO Stopping MOM now...

when running on a system without the key you should see that message, in addition to the actual logic executing.

~bp
they key exist, with the value, but no data in it or one of the data items missing.
key:
HKLM\SYSTEM\CurrentControlSet\services\MOM
 value:
DependOnService
value type:
REG_MULTI_SZ
data:
winmgmt
rpcss
eventlog
 
what am trying to check for is the data and if they don't all exist to run these lines:
if exist winmgmt goto Done
net stop MOM
Echo Y | reg add HKLM\SYSTEM\CurrentControlSet\services\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
net start MOM
Ah, it's starting to make more sense now.  You say you want to check and "if they don't all exist" then run these lines, but then below that it seems to indicate only iof winmgmt does not exist.  Can you clarify, do you want to run the reg add only if there isn't winmgmt, or if all three values you mention are not all there?

~bp
I am ok with just checking for the winmgmt, because the others will always be there.
 
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
that's the one.
 
Thanks a lot billprew for all your hard work.
Excellent.
Great, glad we got that to a useful state, sorry it took a little long.  Thanks for the grade, comment and points.

~bp
you did great man and thanks a lot.