Findstr in Registry

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Findstr in Registry

#1 Post by sourbread » 27 Jul 2012 08:19

Hey everyone, I'm trying to find a solution to this problem. Basically, I'm trying to create an if/else if argument in a batch that does the following:

1.) If the following registry value exist /v AllowRemoteRPC /t REG_DWORD /d 1 in "hklm\system\currentcontrolset\control\terminal server" then move on to :sendmessage
2.) If it does not exist, then prompt the user to run the regfix.bat file

I have something like this (Mind you, I'm still learning the ways of batch scripting, so if it seems newbish I apologize)

:regcheck
echo ===========================================================================
echo Checking Registry of %enduser%
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 2000 >nul
reg query "\\%enduser%\hklm\system\currentcontrolset\control\terminal server" /v AllowRemoteRPC
if %errorlevel% == 0 (
echo ===========================================================================
echo Okay! %enduser% is set for receiving Administrator Messages
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 2000 >nul
cls
goto :sendmessage
) else if %errorlevel% == 1 (
echo ===========================================================================
echo %enduser% does not have the Registry Fix, please run regfix.bat!
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 5000
goto :quit

Should I use a findstr? I've seen a lot of this For /F stuff but honestly, I'm having a hard time finding someone or website explaining to me how I can use it.

Any help is appreciated!

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Findstr in Registry

#2 Post by Squashman » 27 Jul 2012 08:23

) ELSE ( IF .......

sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Re: Findstr in Registry

#3 Post by sourbread » 27 Jul 2012 08:45

No dice :-/

did the following modification


:regcheck
echo ===========================================================================
echo Checking Registry of %enduser%
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 2000 >nul
reg query "\\%enduser%\hklm\system\currentcontrolset\control\terminal server" /v AllowRemoteRPC
if %errorlevel% == 0 (
echo ===========================================================================
echo Okay! %enduser% is set for receiving Administrator Messages
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 2000 >nul
cls
goto :sendmessage
) else ( if %errorlevel% == 1 (
echo ===========================================================================
echo %enduser% does not have the Registry Fix, please run regfix.bat!
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 5000
)
goto :quit

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Findstr in Registry

#4 Post by Squashman » 27 Jul 2012 09:34

You don't need to check the errorlevel after the else. You already know it is not zero if it executes the else.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Findstr in Registry

#5 Post by Ed Dyreen » 27 Jul 2012 10:03

'
I'm not that good as you that I could go without indention :(

Code: Select all

:regcheck

echo ===========================================================================
echo Checking Registry of %enduser%
echo ===========================================================================
ping 172.0.0.1 -n 1 -w 2000 >nul

:: sending stderr to nul device, I'll echo a custom message later...
2>nul reg query "\\%enduser%\hklm\system\currentcontrolset\control\terminal server" /v AllowRemoteRPC &&(

   echo ===========================================================================
   echo Okay! %enduser% is set for receiving Administrator Messages
   echo ===========================================================================
   ping 172.0.0.1 -n 1 -w 2000 >nul
   cls
   goto :sendmessage
) || (
   echo ===========================================================================
   echo %enduser% does not have the Registry Fix, please run regfix.bat!
   echo ===========================================================================
   ping 172.0.0.1 -n 1 -w 5000
)

goto :quit


:sendmessage ...
:quit ...

pause
exit
No need to check errorLevels, reg query only returns True or False.

ps: according to MS, it's not a bug, it's a feature !

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Findstr in Registry

#6 Post by Squashman » 27 Jul 2012 11:07

Good use of the batch operators there Ed.

sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Re: Findstr in Registry

#7 Post by sourbread » 27 Jul 2012 11:55

Works like a charm! Thank you! I'm still trying to learn as much as I can so I can actually be of benefit on these forums rather than mooching :(

Thank you again!

Post Reply