Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listhdds.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF     "%~1"=="" GOTO Syntax
  4. IF NOT "%~2"=="" IF /I NOT "%~2"=="/C" GOTO Syntax
  5. ECHO.%1| FINDSTR /R /C:"[^A-Z0-9_-]" >NUL && GOTO Syntax
  6. PING %~1 2>NUL | FIND "TTL=" >NUL || GOTO Syntax
  7.  
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9.  
  10. SET Computer=%~1
  11.  
  12. :: Check if WMIC.EXE is available
  13. WMIC.EXE /? >NUL 2>&1 && GOTO :WMIC
  14.  
  15. :: Check if DEVCON.EXE is available and if not, offer to download it
  16. SET DevconAvailable=
  17. SET Download=
  18. DEVCON.EXE /? >NUL 2>&1
  19. IF ERRORLEVEL 1 (
  20. 	SET DevconAvailable=No
  21. 	ECHO This batch file requires Microsoft's DEVCON untility.
  22. 	SET /P Download=Do you want to download it now? [y/N] 
  23. )
  24.  
  25. :: Start download if requested
  26. IF /I "%Download%"=="Y" (
  27. 	START "DevCon" "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  28. 	ECHO.
  29. 	ECHO Install the downloaded file and make sure DEVCON.EXE is in the PATH.
  30. 	ECHO Then try again.
  31. )
  32.  
  33. :: Abort if DEVCON.EXE is not available yet
  34. IF "%DevconAvailable%"=="No" (
  35. 	ENDLOCAL
  36. 	GOTO:EOF
  37. )
  38.  
  39. :: Query the remote computer and store the results in variables
  40. :: List IDE and SATA devices first
  41. SET Count=-1
  42. FOR /F "tokens=1,2* delims=[]: " %%A IN ('DEVCON -m:\\%Computer% Find =DiskDrive IDE\DISK* ^| FIND /N ":"') DO (
  43. 	Set /A Count = %%A - 1
  44. 	SET %1.HDD.!Count!=%%C
  45. )
  46. :: Next list SCSI devices
  47. SET IntCnt=%Count%
  48. FOR /F "tokens=1,2* delims=[]: " %%A IN ('DEVCON -m:\\%Computer% Find =DiskDrive SCSI\DISK* ^| FIND /N ":"') DO (
  49. 	Set /A Count = %%A + %IntCnt%
  50. 	SET Model=%%C
  51. 	REM Remove " SCSI Disk Device" from model string
  52. 	SET Model=!Model:~0,-17!
  53. 	REM Remove inconsistent trailing spaces
  54. 	IF "!Model:~-1,1!"==" " SET Model=!Model:~0,-1!
  55. 	SET %1.HDD.!Count!=!Model!
  56. )
  57.  
  58. GOTO :Display
  59.  
  60. :WMIC
  61. SET Count=-1
  62. FOR /F "skip=1 tokens=2 delims=," %%A IN ('WMIC.EXE /Node:%Computer% /Output:STDOUT Path Win32_DiskDrive Where ^(InterfaceType^="SCSI" Or InterfaceType^="IDE"^) Get Model /Format:CSV ^| FIND ","') DO (
  63. 	SET /A Count += 1
  64. 	SET Model=%%A
  65. 	ECHO.!Model! | FIND "SCSI Disk Device" >NUL
  66. 	IF NOT ERRORLEVEL 1 (
  67. 		REM Remove " SCSI Disk Device" from model string
  68. 		SET Model=!Model:~0,-17!
  69. 		REM Remove inconsistent trailing spaces
  70. 		IF "!Model:~-1,1!"==" " SET Model=!Model:~0,-1!
  71. 	)
  72. 	SET %1.HDD.!Count!=!Model!
  73. )
  74.  
  75. :Display
  76. SET /A %1.HDD.Count = %Count% + 1
  77. IF /I "%~2"=="/C" (
  78. 	REM Comma delimited
  79. 	SET Result="%Computer%","!%1.HDD.Count!"
  80. 	FOR /L %%A IN (0,1,!%1.HDD.Count!) DO (
  81. 		IF %%A LSS !%1.HDD.Count! (
  82. 			SET Result=!Result!,"!%1.HDD.%%A!"
  83. 		)
  84. 	)
  85. 	ECHO.!Result!
  86. ) ELSE (
  87. 	REM Default list format
  88. 	SET %1.HDD
  89. )
  90.  
  91. ENDLOCAL
  92. GOTO:EOF
  93.  
  94.  
  95. :Syntax
  96. ECHO.
  97. ECHO ListHDDs.bat,  Version 1.11 for Windows 2000 / XP
  98. ECHO List all harddisk models of the specified computer
  99. ECHO.
  100. ECHO Usage:  LISTHDDS  computer  [ /C ]
  101. ECHO.
  102. ECHO Where:  computer  is the remote computer name or IP address
  103. ECHO         /C        forces comma delimited output (default is list)
  104. ECHO.
  105. ECHO Note:   Requires either WMIC or DEVCON to query the remote computer.
  106. ECHO         If neither is available, you will be prompted to open the DEVCON
  107. ECHO         download page on microsoft.com
  108. ECHO.
  109. ECHO Sample output (default list format):
  110. ECHO %ComputerName%.HDD.0=SAMSUNG HD103UJ
  111. ECHO %ComputerName%.HDD.1=WDC WD10 EACS-22D6B0
  112. ECHO %ComputerName%.HDD.Count=2
  113. ECHO.
  114. ECHO or comma delimited:
  115. ECHO "%ComputerName%","2","SAMSUNG HD103UJ","WDC WD10 EACS-22D6B0"
  116. ECHO.
  117. ECHO Written by Rob van der Woude
  118. ECHO http://www.robvanderwoude.com
  119.  
  120. IF "%OS%"=="Windows_NT" ENDLOCAL
  121.  

page last uploaded: 2017-04-06, 13:33