Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for servers.bat

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

  1. @ECHO OFF
  2. :: Servers.bat, Version 2.02 for Windows NT
  3. :: List disk usage for all drives on all servers in tab delimited format.
  4. :: Written by Rob van der Woude
  5. :: http://www.robvanderwoude.com
  6.  
  7. :: For Windows NT 4 and Windows 2000 only
  8. VER | FIND "Windows NT" >NUL
  9. IF NOT ERRORLEVEL 1 GOTO Begin
  10. VER | FIND "Windows 2000" >NUL
  11. IF NOT ERRORLEVEL 1 GOTO Begin
  12. GOTO VerErr
  13.  
  14. :Begin
  15. TITLE %~nx0
  16. ECHO.>CON
  17.  
  18. :: Make sure command extensions are active
  19. VERIFY OTHER 2>NUL
  20. SETLOCAL ENABLEEXTENSIONS
  21. IF ERRORLEVEL 1 GOTO CmdExtErr
  22.  
  23. :: Check if invalid switch was used, if so display usage
  24. IF NOT [%1]==[] IF /I NOT [%1]==[/H] GOTO Syntax
  25.  
  26. :: Find servers and update the list if necessary
  27. :: The FIND filter skips "servers" whose name start with \\PC
  28. :: Adjust the FIND filter(s) to match your own situation
  29. FOR /F "skip=3 eol=T tokens=1* delims= " %%A IN ('NET VIEW ^| FIND /I /V "\\PC"') DO CALL :AddSrv %%A
  30.  
  31. :: Display header line if /H switch was used
  32. IF /I "%1"=="/H" (>CON ECHO Domain	SrvType	Server	Drive	Free	Size	Used	MB/GB	%% Free	%% Used	FS	Volume)
  33. IF /I "%1"=="/H" (>%~dpn0.dat ECHO Domain	SrvType	Server	Drive	Free	Size	Used	MB/GB	%% Free	%% Used	FS	Volume)
  34.  
  35. :: Check all servers in the list
  36. FOR /F "tokens=*" %%A IN ('TYPE %~dpn0.srv ^| FIND /V ";" ^| SORT') DO CALL :ChkSrv %%A
  37. GOTO End
  38.  
  39. :AddSrv
  40. :: Add server to the list if it isn't listed yet,
  41. :: unless it is listed in the exclusion list
  42. SET AddThisOne=1
  43. IF EXIST %~dpn0.xcl FOR /F "tokens=*" %%a IN ('TYPE %~dpn0.xcl ^| FIND /V ";"') DO IF /I [%%a]==[%1] SET AddThisOne=0
  44. IF EXIST %~dpn0.srv FOR /F "tokens=*" %%a IN ('TYPE %~dpn0.srv ^| FIND /V ";"') DO IF /I [%%a]==[%1] SET AddThisOne=0
  45. IF %AddThisOne%==1 (>> %~dpn0.srv ECHO %1)
  46. GOTO:EOF
  47.  
  48. :ChkSrv
  49. SETLOCAL
  50. SET Server=%1
  51. SET Server=%Server:\=%
  52. TITLE Checking %Server%
  53. :: Determine server type
  54. FOR /F "tokens=1* delims=:" %%K IN ('SRVINFO -ns %1 ^| FIND "NT Type:"') DO SET SrvType=%%L
  55. ECHO.%SrvType% | FIND "NT Member Server" >NUL
  56. IF NOT ERRORLEVEL 1 SET SrvType=SERVER
  57. ECHO.%SrvType% | FIND "NT Advanced Server" >NUL
  58. IF NOT ERRORLEVEL 1 (
  59. 	SET SrvType=BDC
  60. 	SRVINFO -ns %1 | FIND "PDC:" | FIND /I "%1" >NUL
  61. 	IF NOT ERRORLEVEL 1 SET SrvType=PDC
  62. )
  63. :: Determine domain name
  64. FOR /F "tokens=1* delims=: " %%K IN ('SRVINFO -ns %1 ^| FIND "Domain:"') DO SET Domain=%%L
  65. :: Check all drives on the server
  66. FOR /F "tokens=*" %%? IN ('SRVINFO -ns %1 ^| FIND "$"') DO CALL :ChkDrv %%?
  67. ENDLOCAL
  68. GOTO:EOF
  69.  
  70. :ChkDrv
  71. :: Parse drive data
  72. :: Delims is a TAB followed by a space
  73. FOR /F "Tokens=1-4* delims=	 " %%k IN ("%*") DO (
  74. 	SET Drive=%%k
  75. 	SET FileSys=%%l
  76. 	SET Size=%%m
  77. 	SET Free=%%n
  78. 	SET Used=%%o
  79. )
  80. :: Get volume label
  81. :: Delims is a TAB followed by a space
  82. FOR /F "tokens=5* delims=	 " %%k IN ('DIR \\%Server%\%Drive%\*.* ^| FIND /I "\\%Server%\%Drive%" ^| FIND "Volume"') DO SET Volume=%%l
  83. :: Replace $ in drive letter by a colon
  84. SET Drive=%Drive:$=%:
  85. SET /A Round = %Size% / 2
  86. SET /A PFree = 100 * %Free%
  87. SET /A PFree = %PFree% + %Round%
  88. SET /A PFree = %PFree% / %Size%
  89. SET /A PUsed = 100 * %Used%
  90. SET /A PUsed = %PUsed% + %Round%
  91. SET /A PUsed = %PUsed% / %Size%
  92. :: Display drive data
  93. >CON ECHO.%Domain%	%SrvType%	%Server%	%Drive%	%Free%	%Size%	%Used%	MB	%PFree%	%PUsed%	%FileSys%	%Volume%
  94. >>%~dpn0.dat ECHO.%Domain%	%SrvType%	%Server%	%Drive%	%Free%	%Size%	%Used%	MB	%PFree%	%PUsed%	%FileSys%	%Volume%
  95. GOTO:EOF
  96.  
  97. :VerErr
  98. ECHO This batch file was written for Windows NT 4 and 2000 only
  99. GOTO End
  100.  
  101. :CmdExtErr
  102. (ECHO Unable to enable command extensions) 1>&2
  103. GOTO End
  104.  
  105. :Syntax
  106. >CON ECHO Servers.bat, Version 2.02 for Windows NT
  107. >CON ECHO List disk usage for all drives on all servers in tab delimited format.
  108. >CON ECHO Written by Rob van der Woude
  109. >CON ECHO http://www.robvanderwoude.com
  110. >CON ECHO.
  111. >CON ECHO Usage:  SERVERS  [ /H ^| /? ]
  112. >CON ECHO.        /H shows Header line
  113. >CON ECHO.        /? shows this help screen
  114. >CON ECHO.
  115. >CON ECHO This batch file uses 2 additional files:
  116. >CON ECHO %~n0.srv contains a list of all server names to be checked, one server
  117. >CON ECHO             name per line, preceded by double backslashes; if this file
  118. >CON ECHO             does not exist, the batch file will create it.
  119. >CON ECHO %~n0.xcl contains a list of all server names to be excluded, one server
  120. >CON ECHO             name per line, preceded by double backslashes. This file is
  121. >CON ECHO             optional: if it doesn't exist no servers are excluded.
  122. >CON ECHO If a semi-colon is found anywhere in a line in these files, that line
  123. >CON ECHO will be ignored.
  124. >CON ECHO You may also add or adjust FIND filters in line 29 of this batch file
  125. >CON ECHO to prevent workstations from being searched.
  126. >CON ECHO Output is displayed on screen AND stored in a tab delimited ASCII file
  127. >CON ECHO named %~n0.dat.
  128.  
  129. :End
  130. ENDLOCAL
  131.  

page last uploaded: 2017-07-06, 12:37