Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for Natalie Green's createadlist.bat

(view source code of ng_createadlist.bat as plain text)

  1. :: Check for live, non-cluster hosts in current domain
  2.  
  3. @ECHO OFF
  4. :: Get date in YYYYMMDD format - ugly script
  5. CALL :DateParse
  6.  
  7. SET FILE=Serverlist.TXT
  8. ECHO FQDN Filename=%1
  9. ECHO USERDOMAIN=%USERDOMAIN%
  10. :: Check if filename with name of FQDN of trusted domain entered
  11. :: Note that %1 is the FQDN filename, and MyUSERDOMAIN is the TLD portion of FQDN
  12. :: If no FQDN filename specified, then set var for current domain and skip to next section
  13. IF "%1"=="" SET MyUSERDOMAIN=%USERDOMAIN% && GOTO :Main1
  14. :: From file, get FQDN into MyFQDN, and TLD value into MyUserDomain
  15. REM FOR /F %%a IN ('TYPE %1') DO ECHO MyFQDN=%%a
  16. FOR /F "tokens=2 delims=." %%a IN ('TYPE %1') DO SET MyUSERDOMAIN=%%a
  17.  
  18. :Main1
  19. :: Display value and remove appending space(s) from string using this code:
  20. :: http://www.dostips.com/DtTipsStringManipulation.php
  21. SET MyUSERDOMAIN=%MyUSERDOMAIN: =%
  22. ECHO MyUSERDOMAIN=%MyUSERDOMAIN%
  23. SET LOGFILE=%MyUSERDOMAIN%-HostChk_%DateParsed%.csv
  24. ECHO LOGFILE=%LOGFILE%
  25.  
  26. :: 1) Get all non-disabled hosts & OS version in current domain using ADSI
  27. :: Query current domain and put all non-disabled Windows hosts into file serverlist.txt
  28. CSCRIPT //nologo CreateListEnabledHosts.vbs %MyUSERDOMAIN%
  29.  
  30. :: 2) Remove hosts in zombie.txt from Serverlist.txt
  31. FINDSTR /I /V /G:zombie.txt Serverlist.txt > realhosts1.txt
  32. DEL /Q /F /A Serverlist.txt > nul
  33. REM FINDSTR /I /V /G:DCT.txt realhosts1.txt > Serverlist.txt
  34. REN realhosts1.txt Serverlist.txt
  35.  
  36. :: Delete old results file
  37. IF EXIST %LOGFILE% DEL %LOGFILE%
  38.  
  39. :: Get each line into a variable
  40. FOR /F "delims=" %%a IN (%FILE%) DO SET HOSTINFO=%%a && CALL :LOOP1
  41. IF "%1"=="" ECHO Done!
  42. GOTO :EOF
  43.  
  44. :: 3) Ping & WMI auth check
  45. :LOOP1
  46. ECHO.
  47. ECHO hostinfo=%hostinfo%
  48. SET HOSTNAME=
  49. SET ON=0
  50. SET WMIConnect=0
  51. REM SET OS=0
  52. :: Get hostname from string
  53. FOR /F "delims=," %%a IN ("%HOSTINFO%") DO SET HOSTNAME=%%a
  54. ECHO hostname=%HOSTNAME%
  55.  
  56. :: PING -4 forces a IPv4 address and does not error when this option not listed in ping /?
  57. PING -4 -n 1 %HOSTNAME% | FIND /I "Reply from" && SET ON=1
  58.  
  59. :: Setting IP Variable for %HOSTNAME%
  60. :: http://stackoverflow.com/a/13201179/1569434
  61. REM FOR /F "tokens=2" %%f IN ('nslookup %HOSTNAME%') DO SET IP=%%f
  62. FOR /F "tokens=2 delims=[]" %%f IN ('ping -4 -n 1 %HOSTNAME% ^| FIND /I "pinging"') DO SET IP=%%f
  63.  
  64. :: If power is off, return to EOF (i.e., initial For statement), else continue
  65. IF "%ON%"=="0" ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>>%LOGFILE% & GOTO :EOF
  66.  
  67. :: Proceeding to verify access via WMI...
  68. :: Note if %HOSTNAME% contains special characters like '-' or '/' WMIC will
  69. :: generate error "invalid global switch". Remedy is to add single quotes around host per:
  70. :: https://support.quest.com/SolutionDetail.aspx?id=SOL68607
  71. WMIC /node:'%HOSTNAME%' ComputerSystem get status /value | find /i "Status=OK" && SET WMIConnect=1
  72. ECHO WMIConnect=%WMIConnect%
  73. IF "%WMIConnect%"=="0" ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>> %LOGFILE% & GOTO :EOF
  74.  
  75. :: Get OS into variable using this method: http://www.robvanderwoude.com/wmic.php
  76. :: 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
  77. :: Note the carat "^" is an escape character for pipe "|" and for comma "," in the FOR loop
  78. REM FOR /F "tokens=*" %%A IN ('WMIC.EXE /Node:'%HOSTNAME%' Path Win32_OperatingSystem Get /Format:LIST ^|FINDSTR /I /V "version=*"') DO (
  79. REM	SET OS=%%A)
  80.  
  81. ECHO %HOSTINFO%,%IP%,PWR%ON%,WMI%WMIConnect%>> %LOGFILE%
  82.  
  83. SET HOSTINFO=
  84. GOTO :EOF
  85.  
  86.  
  87. :DateParse
  88. :: One of the ugliest scripts required for such a seemingly simple thing is getting dates parsed
  89. :: http://www.robvanderwoude.com/datetimentparse.php
  90. SET Today=%Date: =0%
  91. SET Year=%Today:~-4%
  92. :: Include 1 extra character, which will be either a leading zero or a trailing separator
  93. SET Month=%Today:~-10,3%
  94. :: Remove separator
  95. SET Month=%Month:-=%
  96. SET Month=%Month:/=%
  97. :: Clear leading zeroes
  98. SET /A Month = 100%Month% %% 100
  99. :: And add one again, if necessary
  100. SET /A Month = 100 + %Month%
  101. SET Month=%Month:~-2%
  102. SET Day=%Today:~-7,2%
  103. :: Remove separator
  104. SET Day=%Day:-=%
  105. SET Day=%Day:/=%
  106. :: Clear leading zeroes, as there may be 2 leading zeroes
  107. SET /A Day = 100%Day% %% 100
  108. :: And add one again, if necessary
  109. SET /A Day = 100 + %Day%
  110. SET Day=%Day:~-2%
  111.  
  112. SET DateParsed=%year%%month%%day%
  113. GOTO :EOF
  114.  

page last modified: 2024-02-26; loaded in 0.0125 seconds