:: Check for live, non-cluster hosts in current domain @ECHO OFF :: Get date in YYYYMMDD format - ugly script CALL :DateParse SET FILE=Serverlist.TXT ECHO FQDN Filename=%1 ECHO USERDOMAIN=%USERDOMAIN% :: Check if filename with name of FQDN of trusted domain entered :: Note that %1 is the FQDN filename, and MyUSERDOMAIN is the TLD portion of FQDN :: If no FQDN filename specified, then set var for current domain and skip to next section IF "%1"=="" SET MyUSERDOMAIN=%USERDOMAIN% && GOTO :Main1 :: From file, get FQDN into MyFQDN, and TLD value into MyUserDomain REM FOR /F %%a IN ('TYPE %1') DO ECHO MyFQDN=%%a FOR /F "tokens=2 delims=." %%a IN ('TYPE %1') DO SET MyUSERDOMAIN=%%a :Main1 :: Display value and remove appending space(s) from string using this code: :: http://www.dostips.com/DtTipsStringManipulation.php SET MyUSERDOMAIN=%MyUSERDOMAIN: =% ECHO MyUSERDOMAIN=%MyUSERDOMAIN% SET LOGFILE=%MyUSERDOMAIN%-HostChk_%DateParsed%.csv ECHO LOGFILE=%LOGFILE% :: 1) Get all non-disabled hosts & OS version in current domain using ADSI :: Query current domain and put all non-disabled Windows hosts into file serverlist.txt CSCRIPT //nologo CreateListEnabledHosts.vbs %MyUSERDOMAIN% :: 2) Remove hosts in zombie.txt from Serverlist.txt FINDSTR /I /V /G:zombie.txt Serverlist.txt > realhosts1.txt DEL /Q /F /A Serverlist.txt > nul REM FINDSTR /I /V /G:DCT.txt realhosts1.txt > Serverlist.txt REN realhosts1.txt Serverlist.txt :: Delete old results file IF EXIST %LOGFILE% DEL %LOGFILE% :: Get each line into a variable FOR /F "delims=" %%a IN (%FILE%) DO SET HOSTINFO=%%a && CALL :LOOP1 IF "%1"=="" ECHO Done! GOTO :EOF :: 3) Ping & WMI auth check :LOOP1 ECHO. ECHO hostinfo=%hostinfo% SET HOSTNAME= SET ON=0 SET WMIConnect=0 REM SET OS=0 :: Get hostname from string FOR /F "delims=," %%a IN ("%HOSTINFO%") DO SET HOSTNAME=%%a ECHO hostname=%HOSTNAME% :: PING -4 forces a IPv4 address and does not error when this option not listed in ping /? PING -4 -n 1 %HOSTNAME% | FIND /I "Reply from" && SET ON=1 :: Setting IP Variable for %HOSTNAME% :: http://stackoverflow.com/a/13201179/1569434 REM FOR /F "tokens=2" %%f IN ('nslookup %HOSTNAME%') DO SET IP=%%f FOR /F "tokens=2 delims=[]" %%f IN ('ping -4 -n 1 %HOSTNAME% ^| FIND /I "pinging"') DO SET IP=%%f :: If power is off, return to EOF (i.e., initial For statement), else continue IF "%ON%"=="0" ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>>%LOGFILE% & GOTO :EOF :: Proceeding to verify access via WMI... :: Note if %HOSTNAME% contains special characters like '-' or '/' WMIC will :: generate error "invalid global switch". Remedy is to add single quotes around host per: :: https://support.quest.com/SolutionDetail.aspx?id=SOL68607 WMIC /node:'%HOSTNAME%' ComputerSystem get status /value | find /i "Status=OK" && SET WMIConnect=1 ECHO WMIConnect=%WMIConnect% IF "%WMIConnect%"=="0" ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>> %LOGFILE% & GOTO :EOF :: Get OS into variable using this method: http://www.robvanderwoude.com/wmic.php :: It is REMmed out because it's now pulled directly from AD in the VBS script, above, but could be useful later as a reference :: Note the carat "^" is an escape character for pipe "|" and for comma "," in the FOR loop REM FOR /F "tokens=*" %%A IN ('WMIC.EXE /Node:'%HOSTNAME%' Path Win32_OperatingSystem Get /Format:LIST ^|FINDSTR /I /V "version=*"') DO ( REM SET OS=%%A) ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>> %LOGFILE% SET HOSTINFO= GOTO :EOF :DateParse :: One of the ugliest scripts required for such a seemingly simple thing is getting dates parsed :: http://www.robvanderwoude.com/datetimentparse.php SET Today=%Date: =0% SET Year=%Today:~-4% :: Include 1 extra character, which will be either a leading zero or a trailing separator SET Month=%Today:~-10,3% :: Remove separator SET Month=%Month:-=% SET Month=%Month:/=% :: Clear leading zeroes SET /A Month = 100%Month% %% 100 :: And add one again, if necessary SET /A Month = 100 + %Month% SET Month=%Month:~-2% SET Day=%Today:~-7,2% :: Remove separator SET Day=%Day:-=% SET Day=%Day:/=% :: Clear leading zeroes, as there may be 2 leading zeroes SET /A Day = 100%Day% %% 100 :: And add one again, if necessary SET /A Day = 100 + %Day% SET Day=%Day:~-2% SET DateParsed=%year%%month%%day% GOTO :EOF