WMIC not returning the right errorlevel

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

WMIC not returning the right errorlevel

#1 Post by A_Bobby » 13 Sep 2012 21:23

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!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: WMIC not returning the right errorlevel

#2 Post by foxidrive » 13 Sep 2012 23:16

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
)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: WMIC not returning the right errorlevel

#3 Post by Liviu » 14 Sep 2012 00:49

Or, if wmic is known to not return negative errorlevels, replace "if %errorlevel% equ 0" with "if not errorlevel 1".

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: WMIC not returning the right errorlevel

#4 Post by A_Bobby » 14 Sep 2012 07:08

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.

Post Reply