Medquest
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\CurrentControl Set\servic es\MOM /v DependOnService /t REG_MULTI_SZ
if exist winmgmt goto Done
net stop MOM
Echo Y | reg add HKLM\SYSTEM\CurrentControl Set\servic es\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
net start MOM
:Done
Exit
*END*
any help will be greatly appreciate it.
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\CurrentControl
if exist winmgmt goto Done
net stop MOM
Echo Y | reg add HKLM\SYSTEM\CurrentControl
net start MOM
:Done
Exit
*END*
any help will be greatly appreciate it.
Try this.
~bp
~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
)
ASKER
billprew,
Thanks for your respose. I tride the code you sent me and got this error message:
A" was unexpected at this time.
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
~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
)
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?
Okay, let's try a slight different approach.
~bp
~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
)
ASKER
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
~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
)
ASKER
that's what I got:
Not found 2
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
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
ASKER
they key exist, with the value, but no data in it or one of the data items missing.
key:
HKLM\SYSTEM\CurrentControl Set\servic es\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\CurrentControl Set\servic es\MOM /v DependOnService /t REG_MULTI_SZ /d winmgmt\0rpcss\0eventlog
net start MOM
key:
HKLM\SYSTEM\CurrentControl
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\CurrentControl
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
~bp
ASKER
I am ok with just checking for the winmgmt, because the others will always be there.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that's the one.
Thanks a lot billprew for all your hard work.
Thanks a lot billprew for all your hard work.
ASKER
Excellent.
Great, glad we got that to a useful state, sorry it took a little long. Thanks for the grade, comment and points.
~bp
~bp
ASKER
you did great man and thanks a lot.
ASKER
Registry-Image.jpg