Outputting commands and output to a txt?

June 14, 2011 at 11:28:16
Specs: Windows XP
Basically I'm trying to see if theres a way to output the commands themselves in addition to the output.
I need to log serial #s for pcs, so I have this little batch Im using

set ListFile=hosts.txt

for /f %%a in (%ListFile%) do (
wmic /node:%%a bios get serialnumber
) >> Serials.txt

Hostnames in hosts.txt, works great. Screen shows the hostname then the serial, beautiful.
Open the serials.txt, uhh which goes to who? Bit of a madness.
I hit this roadblock trying to gather a mass of getmac /s mac addresses and I had given up.
Is there a way to do this?


See More: Outputting commands and output to a txt?

Report •

#1
June 14, 2011 at 14:15:24
Untested:

set ListFile=hosts.txt
for /f %%a in (%ListFile%) do (
    for /f "tokens=2 delims==" %%b in ('wmic /node:%%a bios get serialnumber /format:list') do (
        echo %%a's serial number is: %%b>> Serials.txt
    )
) 

A second for loop is performed, parsing the output from the wmic command, which is
surrounded by single quotes. I added the /format:list option, so the output is more
predictable:

SerialNumber=DMKxxxx

Although you don't have to use the /format option, I got into the habit of including it,
and habits are hard to break! This should give you the general idea about parsing the
command output.

Without the /format option, you'd probably want to use the for command's skip option:

set ListFile=hosts.txt
for /f %%a in (%ListFile%) do (
    for /f "skip=1" %%b in ('wmic /node:%%a bios get serialnumber') do (
        echo %%a's serial number is: %%b>> Serials.txt
    )
) 


When your only tool is a hammer, every problem looks like a nail.


Report •

#2
June 15, 2011 at 05:59:35
Worked like a charm. Only drawback I see now is seems some of these are shutdown or not on the network so it errors and skips them.
Any recommendation on having it report in line some like not responding?
Or can it output just the nonreplying hosts to a downhost.txt so I can run that periodically during the day to weed out the ones that come online at different hours.
Thatll trim down these 300 pcs to the handful I may need to physically locate.

Report •

#3
June 16, 2011 at 04:25:46
I've been working with using the output from NET VIEW to create a list of online hosts, but for some reason that I can't explain, the batch file I'm testing with will "get stuck" looking for input from the console when trying to execute WMIC.exe. Once I get around that issue, I'll put up the batch which may satisfy your subsequent request.

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
June 16, 2011 at 17:07:13
Assuming you're in a domain environment, give this a shot to create a list of online computers to work from and pass to WMIC (Online.txt).


@echo off
setlocal ENABLEDELAYEDEXPANSION

for /f "tokens=*" %%a in ('DSQUERY computer -limit 2250 -name * -o rdn') do echo %~a%% >>comps.txt

for /f %%a in (comps.txt) do (
ping %%a -n 1 -w 100 >nul
if !errorlevel! equ 0 (Echo %%a >>Online.txt.) else Echo %%a Offline.txt
)


Report •

Ask Question