Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch registry query to take action on condition

Posted on 2012-08-14
9
Medium Priority
?
514 Views
Last Modified: 2012-08-16
Hello,

I have a batch file I'm working on that queries the registry to see if a particular version of software is installed. I think I'm on the right track but am having trouble getting it to evaluate different versions. It seems to detect them all and even if nothing is found in the registry it processes each line.

Any ideas on how I can fix this?

FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Sunbelt Software\Sunbelt Enterprise Agent" /V "Version" 2^>NUL ^| FIND "REG_SZ"') DO SET VIPREVer=%%B
If VIPREVer == "3.1.2848" goto 312848
If VIPREVer == "4.0.3907" goto 403907 
If VIPREVer == "4.0.4205" goto 404205 

goto eof

:312848
msiexec /x "\\Server1\Applications\Sunbelt\Desktops\SBEAgent-HPEaton_Desktops.msi" /QN

:403907
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_403907.msi" /QN

:404205
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_401_EN-HPE Desktops.msi" /QN


:eof

Open in new window


Thanks
0
Comment
Question by:hpops
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 38293930
You forgot you GOTO EOFs after each branch. See the example below:

FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Sunbelt Software\Sunbelt Enterprise Agent" /V "Version" 2^>NUL ^| FIND "REG_SZ"') DO SET VIPREVer=%%B
If VIPREVer == "3.1.2848" goto 312848
If VIPREVer == "4.0.3907" goto 403907 
If VIPREVer == "4.0.4205" goto 404205 
goto eof

:312848
msiexec /x "\\Server1\Applications\Sunbelt\Desktops\SBEAgent-HPEaton_Desktops.msi" /QN
goto :eof

:403907
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_403907.msi" /QN
goto :eof

:404205
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_401_EN-HPE Desktops.msi" /QN
goto :eof

    ::
    ::
    ::

:eof
   

Open in new window

0
 

Author Comment

by:hpops
ID: 38294523
Thank you. This helped but now I'm having trouble with the script evaluating properly. It seems that the item If VIPREVer == "3.1.2848" goto 312848 is always detected. This even occurs even when the registry key is not present on the system. I'm still working on it but any ideas on how to fix the evaluation process would be greatly appreciated.
0
 

Author Comment

by:hpops
ID: 38297377
I am still struggling to get this script to work. Can anyone help?
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
LVL 11

Expert Comment

by:paultomasi
ID: 38298441
apologies. fell asleep whlie reviewing your reply.... just arrived home again so will give it another look in a mo
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38298466
sorry i missed the obvious...

put percent characters around your variable. Also, note the double-quotes as well, so both sides of the '==' sign in the IF conditional statement match exactly. ie, "%VIPREVer%" like this:

FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Sunbelt Software\Sunbelt Enterprise Agent" /V "Version" 2^>NUL ^| FIND "REG_SZ"') DO SET VIPREVer=%%B
If "%VIPREVer%"=="3.1.2848" goto 312848
If "%VIPREVer%"=="4.0.3907" goto 403907 
If "%VIPREVer%"=="4.0.4205" goto 404205 
goto :eof

:312848
msiexec /x "\\Server1\Applications\Sunbelt\Desktops\SBEAgent-HPEaton_Desktops.msi" /QN
goto :eof

:403907
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_403907.msi" /QN
goto :eof

:404205
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_401_EN-HPE Desktops.msi" /QN
goto :eof

    ::
    ::
    ::

:eof

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38298529
Another way to do it is to substitute out the full-stop characters from '3.1.2848', '4.0.3907' and '4.0.4205' and use them in a single GOTO statement like this:

for /f "tokens=2*" %%a in ('reg query "HKLM\Software\Sunbelt Software\Sunbelt Enterprise Agent" /v "Version" 2^>nul ^| find "REG_SZ"') do set VIPREVer=%%b

goto %VIPREVer:.=%

:312848
msiexec /x "\\Server1\Applications\Sunbelt\Desktops\SBEAgent-HPEaton_Desktops.msi" /QN
goto :eof

:403907
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_403907.msi" /QN
goto :eof

:404205
msiexec /x "\\Server1\Applications\Sunbelt\HPE_Desktops_4.0.4205\SBVEA_401_EN-HPE Desktops.msi" /QN
goto :eof

Open in new window

Also, you don't strictly need a label named ':eof' as it's an inbuilt batch language end-of-file position.
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 2000 total points
ID: 38298575
Personally, if I had to code this for myself I would do something along the following lines (providing all info is correctly stated):

@echo off
setlocal enabledelayedexpansion

set "AppPth=\\Server1\Applications\Sunbelt"
set "AppKey=HKLM\Software\Sunbelt Software\Sunbelt Enterprise Agent"

set "msi[3.1.2848]=%AppPth%\Desktops\SBEAgent-HPEaton_Desktops.msi"
set "msi[4.0.3907]=%AppPth%\HPE_Desktops_4.0.4205\SBVEA_403907.msi"
set "msi[4.0.4205]=%AppPth%\HPE_Desktops_4.0.4205\SBVEA_401_EN-HPE Desktops.msi"

for /f "tokens=2*" %%a in ('reg query "%AppKey%" /v "Version" 2^>nul ^| find "REG_SZ"') do msiexec /x "!msi[%%b]!" /qn

Open in new window

However, this is an advanced programmer's approach (as is, to a lesser extent, the previous reply) therefore, you should stick to what you know best (see http:#a38298466)
0
 

Author Closing Comment

by:hpops
ID: 38300023
The advanced programming example works best for me. Thank you so much for the excellent examples. You are very talented in batch scripting.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38300164
You're very welcome my friend !! And thank you for the kind comment.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

578 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