listbatch-filetext-fileshotfixsystem-information

Parsing KBs from systeminfo in batch


Question: What is a method that can parse out just the hotfix KB entries in a Windows systeminfo command report.

Background: I have many windows systems that I need to get the list of installed KBs from. I can't install any software on the systems, so I've resolved to use a batch file.

I would like to have a list of KBs as the output, saved to a file " .txt"

My Current Method: So far, I can get the full list from systeminfo and put the entire output into a text file named with the device's host name and date and stick it in a directory called Systeminfo Saves. That works well enough for me, but I'm really only interested in the KBs and I am hoping to use that output to compare it to a list of validated KBs so I can get a list of KBs that need installed.

I've been messing around with for statements and tokens and delimiters to narrow down the list to just the KB files, but without success.

Here's my current code:

@echo off
REM Get date and host and set it to sFileName
REM -----------------------------------------
setlocal enabledelayedexpansion
set ssDate=%date%
set sHost=%computername%
set sDate=!ssDate:/=-!
REM Remove the day of the week
set sDate=%sDate:~-10%
set sFileName=%sHost% %sDate%.txt
REM -----------------------------------------


REM - Save system info to hostname and date -
REM -----------------------------------------
systeminfo > "Systeminfo Saves/%sFileName%"
REM
REM PUT FOR STATEMENT HERE FOR OUTPUT OF JUST KBs
REM -----------------------------------------

My next step that I haven't been able to pull off is to parse out the installed KBs into a file.

For statements have thrown me for a loop (pun mildly intended). I've tried this:

REM OUTPUT a list of KBs
SET count=1
set lookieloo = "]: KB"
FOR /F "tokens=* USEBACKQ" %%F IN (`systeminfo`) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
  REM See if "]: KB" is in the resultant string
  if not x%%F:"]: KB"=%==x%%F echo %%F >> "Systeminfo Saves/%sFileName%"
)

I'm splicing together answers that I don't fully understand from other Stack Overflow answers that partially answer my question.

The last save to file I'm fairly sure won't work . The "]: KB" doesn't work at all. I tried using the variable lookieloo to get that statement to work. I didn't get an error, but I didn't get a list of KBs either.

So, the question is: What would a batch program look like to export a text file of installed KBs on a system using the systeminfo command?


Solution

  • From LotPings comment, I was able to put together a full program that extracts the installed KBs using just batch.

    @setlocal enableextensions enabledelayedexpansion
    @echo off
    REM Get date and host and set it to sFileName
    REM -----------------------------------------
    set ssDate=%date%
    set sHost=%computername%
    
    REM Get the directory and make it if it doesn't exist
    set sDir=Systeminfo Saves
    if not exist "%sDir%\" mkdir "%sDir%\"
    set sDate=!ssDate:/=-!
    
    REM Remove the day of the week
    set sDate=%sDate:~-10%
    
    REM Compile the full file name with directory
    set sFileName=%sDir%^\%sHost% %sDate%.txt
    REM -----------------------------------------
    echo %sFileName%
    
    REM -- Extract KB information and save to file --
    (For /f "tokens=2delims=: " %%A in ('systeminfo^|findstr "\[[0-9][0-9]*\]:.KB[0-
    9]*"') Do @Echo:%%A)>"%sFileName%"
    REM ---------------------------------------------