Page 1 of 1

WMIC not returning the right errorlevel

Posted: 13 Sep 2012 21:23
by A_Bobby
Here's a simple script that reads a list.txt file for KB patches and queries local machine using WMIC to see if its installed
@echo on
setlocal enabledelayedexpansion
for /f %%i in (list.txt) do (
wmic qfe get hotfixid |findstr /i "%%i"
if %errorlevel% equ 0 @echo %%i found
)
I am always getting %errorlevel% 0 .. why?
Here's sample output

C:\Temp>(
wmic qfe get hotfixid | findstr /i "KB961371"
if 0 EQU 1
)

C:\Temp>(
wmic qfe get hotfixid | findstr /i "KB2584146"
if 0 EQU 1
)
KB2584146

C:\Temp>(
wmic qfe get hotfixid | findstr /i "KB2585542"
if 0 EQU 1
)
KB2585542

The second and third time it should be zero and it is but the first time it doesn't find the patch but still returns zero.

Thanks in advance for help!

Re: WMIC not returning the right errorlevel

Posted: 13 Sep 2012 23:16
by foxidrive
Try this:

Code: Select all

@echo on
setlocal enabledelayedexpansion
for /f %%i in (list.txt) do (
wmic qfe get hotfixid |findstr /i "%%i"
if !errorlevel! equ 0 @echo %%i found
)

Re: WMIC not returning the right errorlevel

Posted: 14 Sep 2012 00:49
by Liviu
Or, if wmic is known to not return negative errorlevels, replace "if %errorlevel% equ 0" with "if not errorlevel 1".

Re: WMIC not returning the right errorlevel

Posted: 14 Sep 2012 07:08
by A_Bobby
Thanks foxidrive. !errorlevel! worked. Even though I cannot see the output (it just prints !errorlevel! on screen when echo is on) it apparently picks up the right patches.