Avatar of Medquest
Medquest
Flag 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.
ProgrammingMicrosoft DOS

Avatar of undefined
Last Comment
Medquest

8/22/2022 - Mon
Medquest

ASKER
sorry forgot to add this image.
Registry-Image.jpg
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

Medquest

ASKER
billprew,
Thanks for your respose. I tride the code you sent me and got this error message:
A"  was unexpected at this time.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bill Prew

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

Medquest

ASKER
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?
Bill Prew

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Medquest

ASKER
now it's not doing anything no more.
Bill Prew

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

Medquest

ASKER
that's what I got:
Not found 2
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bill Prew

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
Medquest

ASKER
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
Bill Prew

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Medquest

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Medquest

ASKER
that's the one.
 
Thanks a lot billprew for all your hard work.
Medquest

ASKER
Excellent.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

Great, glad we got that to a useful state, sorry it took a little long.  Thanks for the grade, comment and points.

~bp
Medquest

ASKER
you did great man and thanks a lot.