I have a task to query some hostnames for their serial number stored in the BIOS. I have the hostnames in a text file gets processed with a for loop, the clients are queried for the serial number with another for loop, and the result is directed into another text file.
This works no problem if the host is offline or doesn't respond. The hostname is written to the text file and the serial number is blank, and the script then moves on to the next entry in the input file. But if the host responds and a serial number is stored in the variable, the script initially writes to the output file as it should, but then it writes another line with the same hostname and a blank serial number before moving onto the next line in the input file.
@ECHO OFF
set list=c:\temp\input.txt
for /F %%C in (%list%) do (
for /F "skip=1" %%S in ('wmic /node:%%C bios get serialnumber') do (
echo %%C - %%S >>c:\temp\output.txt)
)
pause
This results in output that looks like this:
DC6068WA00829 -
DC6054WA00178 -
DC6061WA00065 - R93NSI3
DC6061WA00065 -
DC6061WA00064 - R0KBN3S
DC6061WA00064 -
DC6023LA034284 -
DC6038LA034272 -
As you can see, when the host doesn't respond because it's offline or can't be contacted for whatever reason, it moves on to the next one just as it should. But when one responds, it immediately writes another line with just the hostname and a dash.
I've tried making the write command to the output file as a separate subroutine call, I've tried removing the writing of the %%S variable to see if that was interfering in some way, I've tried saving the results to different variables before outputting them to the file, but nothing makes any difference.
I can clean up the resulting file without too much issue. I mostly just want to know what I'm missing that would be causing this to happen.
Any help or insight would be appreciated.
/FailFast, (which isOffby default), can be used. It willpingeachnodeand only send theWMI commands to those which respond, i.e.WMIC /Node:@%%C /FailFast:On BIOS "Where SerialNumber Is Not Null" Get SerialNumber./FailFast. That speeds it up quite a bit.