Link to home
Start Free TrialLog in
Avatar of hpops
hpops

asked on

Batch registry query to take action on condition

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
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of hpops
hpops

ASKER

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.
Avatar of hpops

ASKER

I am still struggling to get this script to work. Can anyone help?
apologies. fell asleep whlie reviewing your reply.... just arrived home again so will give it another look in a mo
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

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.
ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of hpops

ASKER

The advanced programming example works best for me. Thank you so much for the excellent examples. You are very talented in batch scripting.
You're very welcome my friend !! And thank you for the kind comment.