@ECHO OFF
SETLOCAL
Rem Configure the script as follows
SET INFILE=allcomps.txt
SET OFFLINEFILE=dcoff.txt
SET ONLINEFILE=dcon.txt
SET BADFILE=bad.txt
Rem Configuration ends here. Do not modify below.
ECHO This will check if the specified computers are online.
ECHO.
ECHO Configuration (Edit %0 to change):
ECHO Input File: %INFILE%
ECHO Offline File: %OFFLINEFILE%
ECHO Online File: %ONLINEFILE%
ECHO Bad File: %BADFILE%
ECHO.
SET /P ANS=Do you wish to continue? Y/N
IF /I "%ANS%" NEQ "Y" GOTO:EOF
IF NOT EXIST %INFILE% (
echo %INFILE% does not exist. Please export it as tab delimited from an ADUC query.
GOTO:EOF
)
SORT %INFILE% /O %INFILE%s
FOR /F "skip=1 tokens=1 delims=, " %%A IN (%INFILE%s) DO CALL:CheckOnline %%A
DEL %INFILE%s
GOTO:EOF
:CheckOnline
title Checking %~1
PING %~1 -n 1 -w 1500 > NUL 2>&1
if "%ERRORLEVEL%" NEQ "0" (
ECHO %~1 is offline.
ECHO %~1>> %OFFLINEFILE%
) ELSE (
ECHO %~1 is online, verifying...
CALL:VerifyName %~1
)
GOTO:EOF
:VerifyName
title Verifying %~1
Rem The IP step is necessary. nbtstat returns bad info when used against the name.
FOR /F "tokens=3 delims=[] " %%A in ('PING -n 1 -w 1500 %~1 ^| FINDSTR -R "\[*\]"') do (SET VIP=%%A)
FOR /F "tokens=1" %%A in ('NBTSTAT -A %VIP% ^| FIND "UNIQUE" ^| FIND "<20>"') do (SET VNAME=%%A)
IF /I "%~1" EQU "%VNAME%" (
ECHO %~1 confirmed!
ECHO %~1>> %ONLINEFILE%
) ELSE (
ECHO %~1 is bad.
ECHO %~1>> %BADFILE%
)
GOTO:EOF