Search code examples
windowsbatch-filevariablescmdwmic

Reporting Computer descriptions or not using WMIC


I am making a script detect for presence of certain programs on a list of PC's and then report back the hostname, software list and the description for easier locating. Everything is fine but I'm having trouble tidying up the output when "no" description is found. Where as before I would get the info needed in a tidy list, when there is no description, it just reports ECHO is on. 'or' ECHO is off.

here is my code

title Problem Software Itinary
echo.
echo List of PC Names goes here Plugins\hostnames.txt
echo.
echo Press a key to start...
pause>nul
CLS
echo.
break>Plugins\Logs\Results.txt
for /f "usebackq tokens=*" %%A in (Plugins\hostnames.txt) do (
echo ============================================= & echo =============================================>>Plugins\Logs\Results.txt
echo Scanning PC %%A... 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
if exist "\\%%A\C$\FOO" echo FOO PROG Installed 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
if exist "\\%%A\C$\FOO2" echo FOO 2 PROG Installed 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
for /f "tokens=2 Delims==" %%B in ('WMIC /NODE:"%%A" os get description /VALUE ^| find "Description"') Do Echo %%B 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
echo ============================================= & echo =============================================>>Plugins\Logs\Results.txt
)
start "" Plugins\Logs\Results.txt
echo.
echo Done! Press any key to return to Menu...
pause>nul
goto :audit

Here is example of it working and the echo problem:-

=============================================
=============================================
Scanning PC TESTPC1...  
FOO PROG Installed  
GP-B82047-ROOM 4  
=============================================
=============================================
Scanning PC TESTPC2...  
FOO 2 PROG Installed  
ECHO is on.
=============================================
=============================================

I would prefer to have something echo back like "No Description Found"

to cure it I have tried setting a desc variable to %%B (set desc=%%b) (in the wmic line) (instead of Do Echo %%B) and then if defined desc (echo desc) ELSE (echo no description available). However that doesnt work either, same issue. Also I tried with delayed expansions enabled and no luck there.

What am I missing - thanks !!?


Solution

  • You actually have part of the solution in your script already, or at least one possible solution:

    for /f "tokens=2 Delims==" %%B in ('WMIC /NODE:"%%A" os get description /VALUE ^| find "Description"') Do Echo %%B 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
    

    If this echo could be empty, just change it to

    for /f "tokens=2 Delims==" %%B in ('WMIC /NODE:"%%A" os get description /VALUE ^| find "Description"') Do Echo.%%B 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
    

    Note the . after the echo. That'll suppress the error message from echo.

    Also, note, it's generally better to use some character other than . for this, since if you have the misfortune of running on a machine with a program named echo in the path, the results can be unpredictable. : works well for this, so:

    for /f "tokens=2 Delims==" %%B in ('WMIC /NODE:"%%A" os get description /VALUE ^| find "Description"') Do Echo:%%B 2>&1 | Plugins\tee -a Plugins\Logs\Results.txt
    

    Will accomplish the same goal.