Reg.exe

G

Guest

I am trying to determine if a specific patch has been applied on a client machine. I am using Reg Query in a batch file to look for the patch. Here is the problem I am running into. I am trying to use error level to branch out to either patched or unpatched. But no matter what error level is returned it always continues on down the batch file. Here is the sample

REG QUERY "HKLM\SOFTWARE\Microsoft\Updates\Windows 2000\SP5\KB828128"
if not errorlevel 0 goto nopatc
echo %1 Patched

it always says the machine is patched, even it if does not find the specific patch.

Any help would be highly appreciated

Thanks in advance
David.
 
D

David Candy

If it is not = 0 or higher goto nopatch.
It will never never go to nopatch

Use this format in batch files
If errorlevel n if not errorlevel n+1 <command to do>Although in your case I'd just go If errorlevel 1 goto nopatch
 
G

Guest

Thanks Austin, Your solution worked like a charm. Strange though. Here is the batch file I have cobbled up
@echo of
:: Sca
:: Donot waste time if machine not pingabl
ping -n 1 %1 | find "TTL
if errorlevel 1 goto norespons
REG QUERY "HKLM\SOFTWARE\Microsoft\Updates\Windows 2000\SP5\KB828028"
if %errorlevel% GTR 0 goto nopatc
echo %1 Patched
goto don

:: Messages Section
:nopatc
echo %1 Unpatche
goto don
:Norespons
echo %1 Unreacheabl
echo %1 Unreacheable >> NoResponse.tx
goto don

:don

So u see, I have use errorlevel without enclosing it with % % and have branched the result based on If errorlevel not 0 for the ping command, yet for the reg query I had to use %errorlevel% and grt 0 to branch out!! I do not understand, but it works.

If anybody can explain the reason for the difference, I wld appreciate it

However, Thanks Austin again for your help

David.
 
D

David Candy

I already explained it.
If errorlevel 1
means
If errorlevel is 1 or more.

Not 0 means not 0 or more, that will never be true unless errorlevel is a negative number which it's not.
 
M

Matthias Tacke

:

So u see, I have use errorlevel without enclosing it with % % and have
branched the result based on If errorlevel not 0 for the ping command,
yet for the reg query I had to use %errorlevel% and grt 0 to branch
out!! I do not understand, but it works. If anybody can explain the
reason for the difference, I wld appreciate it. However, Thanks Austin
again for your help.
David.


Hello David,
see the help "if /?"

IF [NOT] ERRORLEVEL

is the old compatible way dealing with errorlevel in a descending manner.
To overcome that fuss the errorlevel is copied to the environment var
%ERRORLEVEL%. You may compare the value and even construct labels with
it. You can echo it directly without knowing the range.
See this example batch.

01. ::RetErrLvl.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::
02. @echo off & setlocal
03. :: Generating and checking Errlorlevels 1 to 16
04.
05. :: Using Variable %ERRORLEVEL% for every possible value
06. for /L %%A in (1,1,16) do (
07. Call :RetErrLvl %%A
08. call echo Errorlevel %%ERRORLEVEL%%
09. )
10.
11. :: Using old dos compatible method of descending numbers
12. :: only for the numbers up to 16.
13. :: The usage of goto inside for loops is unreliable.
14. :Loop
15. set /A ErrLvl+=1
16. call :RetErrLvl %ErrLvl%
17. if ERRORLEVEL 16 echo ERRORLEVEL 16 & goto Label
18. if ERRORLEVEL 15 echo ERRORLEVEL 15 & goto Label
19. if ERRORLEVEL 14 echo ERRORLEVEL 14 & goto Label
20. if ERRORLEVEL 13 echo ERRORLEVEL 13 & goto Label
21. if ERRORLEVEL 12 echo ERRORLEVEL 12 & goto Label
22. if ERRORLEVEL 11 echo ERRORLEVEL 11 & goto Label
23. if ERRORLEVEL 10 echo ERRORLEVEL 10 & goto Label
24. if ERRORLEVEL 9 echo ERRORLEVEL 9 & goto Label
25. if ERRORLEVEL 8 echo ERRORLEVEL 8 & goto Label
26. if ERRORLEVEL 7 echo ERRORLEVEL 7 & goto Label
27. if ERRORLEVEL 6 echo ERRORLEVEL 6 & goto Label
28. if ERRORLEVEL 5 echo ERRORLEVEL 5 & goto Label
29. if ERRORLEVEL 4 echo ERRORLEVEL 4 & goto Label
30. if ERRORLEVEL 3 echo ERRORLEVEL 3 & goto Label
31. if ERRORLEVEL 2 echo ERRORLEVEL 2 & goto Label
32. if ERRORLEVEL 1 echo ERRORLEVEL 1 & goto Label
33. if ERRORLEVEL 0 echo ERRORLEVEL 0 & goto Label
34. :Label
35. if %ErrLvl% LSS 16 goto :Loop
36.
37. goto :EOF
38. :RetErrLvl
39. exit /b %1
40. ::RetErrLvl.cmd:::::::::::::::::::::::::::::::::::::::::::::::::

The resulting output is very similar, the expenditure not.:::
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top