Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for disktypes.vbs

(view source code of disktypes.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrCDR( ), arrHDC( ), arrHDD( ), arrHost( )
  4. Dim blnAlignOutput, blnDebug
  5. Dim i, intDescLen, intSizeLen, intValidArgs, j, lngSize
  6. Dim colItems, objItem, objWMIService
  7. Dim strComputer, strInterface, strMsg, strSize
  8.  
  9. blnAlignOutput = True
  10. blnDebug = False
  11. strComputer = "."
  12. intValidArgs = 0
  13.  
  14. ' Parse command line
  15. With WScript.Arguments
  16. 	If .Count         > 3 Then Syntax
  17. 	If .Unnamed.Count > 1 Then Syntax
  18. 	If .Unnamed.Count = 1 Then
  19. 		If strComputer <> "." Then
  20. 			Syntax
  21. 		Else
  22. 			strComputer = .Unnamed(0)
  23. 			intValidArgs = intValidArgs + 1
  24. 		End If
  25. 	End If
  26. 	If .Named.Exists( "HELP" ) Then Syntax
  27. 	If .Named.Exists( "?" ) Then Syntax
  28. 	If .Named.Exists( "D" ) Then
  29. 		blnDebug = True
  30. 		intValidArgs = intValidArgs + 1
  31. 	End If
  32. 	If .Named.Exists( "R" ) Then
  33. 		If strComputer <> "." Then
  34. 			Syntax
  35. 		Else
  36. 			strComputer = .Named.Item( "R" )
  37. 			intValidArgs = intValidArgs + 1
  38. 		End If
  39. 	End If
  40. 	If .Named.Exists( "T" ) Then
  41. 		blnAlignOutput = False
  42. 		intValidArgs = intValidArgs + 1
  43. 	End If
  44. 	' Check if more arguments were passed than the ones we recognized
  45. 	If intValidArgs <> .Count Then
  46. 		WScript.Echo vbCrLf & "ERROR: Invalid command line argument(s)"
  47. 		Syntax
  48. 	End If
  49. End With
  50.  
  51. If Trim( strComputer ) = "" Then
  52. 	WScript.Echo vbCrLf & "ERROR: Specify a computer name when using the /R switch"
  53. 	Syntax
  54. End If
  55.  
  56. On Error Resume Next
  57. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  58. If Err Then
  59. 	WScript.Echo vbCrLf & "ERROR: " & Err.Description
  60. 	Syntax
  61. End If
  62. On Error Goto 0
  63.  
  64. ' Query HDD properties and save results in HDD array
  65. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_DiskDrive" )
  66. ReDim arrHDD( 3, colItems.Count - 1 )
  67. For Each objItem in colItems
  68. 	With objItem
  69. 		lngSize = 0
  70. 		strSize = "0   "
  71. 		If Not IsNull( objItem.Size ) Then
  72. 			lngSize = CInt( .Size / 1073741824 ) ' divide by 1 GB
  73. 			If lngSize > 0 Then
  74. 				strSize = CStr( lngSize ) & " GB"
  75. 			Else
  76. 				lngSize = CInt( .Size / 1048576 ) ' divide by 1 MB
  77. 				strSize = CStr( lngSize ) & " MB"
  78. 			End If
  79. 		End If
  80. 		If IsNull( .InterFaceType ) Or .InterfaceType = "" Then
  81. 			If Left( .PnpDeviceID, 3 ) = "USB" Then
  82. 				strInterface = "USB"
  83. 			Else
  84. 				strInterface = "-"
  85. 			End If
  86. 		Else
  87. 			strInterface = .InterfaceType
  88. 		End If
  89. 		arrHDD( 0, .Index ) = .Caption
  90. 		arrHDD( 1, .Index ) = .PnpDeviceID
  91. 		arrHDD( 2, .Index ) = strSize
  92. 		arrHDD( 3, .Index ) = strInterface
  93. 	End With
  94. Next
  95.  
  96. ' Query CDR properties and save results in CDR array
  97. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_CDROMDrive" )
  98. ReDim arrCDR( 3, colItems.Count - 1 )
  99. i = 0
  100. For Each objItem in colItems
  101. 	With objItem
  102. 		If Left( .PnpDeviceID, 3 ) = "USB" Then
  103. 			strInterface = "USB"
  104. 		Else
  105. 			strInterface = "-"
  106. 		End If
  107. 		arrCDR( 0, i ) = .Caption
  108. 		arrCDR( 1, i ) = .PnpDeviceID
  109. 		arrCDR( 2, i ) = .Drive
  110. 		arrCDR( 3, i ) = strInterface
  111. 	End With
  112. 	i = i + 1
  113. Next
  114.  
  115. ' Query harddisk and USB controller properties and save results in HDC array
  116. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_IDEController" )
  117. ReDim arrHDC( 2, colItems.Count - 1 )
  118. i = 0
  119. For Each objItem in colItems
  120. 	With objItem
  121. 		arrHDC( 0, i ) = UCase( .Caption & " (" & .Description & ")" )
  122. 		arrHDC( 1, i ) = .PNPDeviceID
  123. 		' This is one of two "weakest links" in this script: differentiating between IDE (PATA) and SATA
  124. 		' depends on the words SATA or S-ATA or Serial ATA in the harddisk controller's description field
  125. 		If InStr( arrHDC( 0, i ), "SATA" ) Then
  126. 			arrHDC( 2, i ) = "SATA"
  127. 		ElseIf InStr( arrHDC( 0, i ), "S-ATA" ) Then
  128. 			arrHDC( 2, i ) = "SATA"
  129. 		ElseIf InStr( arrHDC( 0, i ), "SERIAL ATA" ) Then
  130. 			arrHDC( 2, i ) = "SATA"
  131. 		Else
  132. 			arrHDC( 2, i ) = "IDE"
  133. 		End If
  134. 	End With
  135. 	i = i + 1
  136. Next
  137. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_SCSIController" )
  138. ReDim Preserve arrHDC( 2, UBound( arrHDC, 2 ) + colItems.Count )
  139. For Each objItem in colItems
  140. 	With objItem
  141. 		arrHDC( 0, i ) = UCase( .Caption & " (" & .Description & ")" )
  142. 		arrHDC( 1, i ) = .PNPDeviceID
  143. 		arrHDC( 2, i ) = "SCSI"
  144. 	End With
  145. 	i = i + 1
  146. Next
  147. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_USBController" )
  148. ReDim Preserve arrHDC( 2, UBound( arrHDC, 2 ) + colItems.Count )
  149. For Each objItem in colItems
  150. 	With objItem
  151. 		arrHDC( 0, i ) = UCase( .Caption & " (" & .Description & ")" )
  152. 		arrHDC( 1, i ) = .PNPDeviceID
  153. 		' This is the second "weakest links" in this script: differentiating between "classic" USB and USB 3.0 depends on the words
  154. 		' "USB 3" in the harddisk controller's description field, or its PnP Device ID starting with "NUSB3" (NEC/Renesas only)
  155. 		If InStr( arrHDC( 0, i ), "USB 3" ) Then
  156. 			arrHDC( 2, i ) = "USB3"
  157. 		ElseIf Left( arrHDC( 0, i ), 5 ) = "NUSB3" Then
  158. 			arrHDC( 1, i ) = "USB3"
  159. 		Else
  160. 			arrHDC( 2, i ) = "USB"
  161. 		End If
  162. 	End With
  163. 	i = i + 1
  164. Next
  165.  
  166. ' Query hosted drives and save results in Host array
  167. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_IDEControllerDevice" )
  168. ReDim arrHost( 2, colItems.Count - 1 )
  169. i = 0
  170. For Each objItem in colItems
  171. 	With objItem
  172. 		arrHost( 0, i ) = Replace( .Antecedent, "\\", "\", 3 )
  173. 		arrHost( 1, i ) = Replace( .Dependent,  "\\", "\", 3 )
  174. 	End With
  175. 	i = i + 1
  176. Next
  177. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_SCSIControllerDevice" )
  178. ReDim Preserve arrHost( 2, UBound( arrHost, 2 ) + colItems.Count )
  179. For Each objItem in colItems
  180. 	With objItem
  181. 		arrHost( 0, i ) = Replace( .Antecedent, "\\", "\", 3 )
  182. 		arrHost( 1, i ) = Replace( .Dependent,  "\\", "\", 3 )
  183. 	End With
  184. 	i = i + 1
  185. Next
  186. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_USBControllerDevice" )
  187. ReDim Preserve arrHost( 2, UBound( arrHost, 2 ) + colItems.Count )
  188. For Each objItem in colItems
  189. 	With objItem
  190. 		arrHost( 0, i ) = Replace( .Antecedent, "\\", "\", 3 )
  191. 		arrHost( 1, i ) = Replace( .Dependent,  "\\", "\", 3 )
  192. 	End With
  193. 	i = i + 1
  194. Next
  195.  
  196. ' Check interface per hosted disk
  197. For i = 0 To UBound( arrHost, 2 )
  198. 	For j = 0 To UBound( arrHDC, 2 )
  199. 		If InStr( arrHost( 0, i ), arrHDC( 1, j ) ) Then
  200. 			arrHost( 2, i ) = arrHDC( 2, j )
  201. 		End If
  202. 	Next
  203. Next
  204.  
  205. ' Match hosted disks array with HDD array
  206. For i = 0 To UBound( arrHDD, 2 )
  207. 	For j = 0 To UBound( arrHost, 2 )
  208. 		If InStr( arrHost( 1, j ), arrHDD( 1, i ) ) Then
  209. 			arrHDD( 3, i ) = arrHost( 2, j )
  210. 		End If
  211. 	Next
  212. Next
  213. For i = 0 To UBound( arrCDR, 2 )
  214. 	For j = 0 To UBound( arrHost, 2 )
  215. 		If InStr( arrHost( 1, j ), arrCDR( 1, i ) ) Then
  216. 			arrCDR( 3, i ) = arrHost( 2, j )
  217. 		End If
  218. 	Next
  219. Next
  220.  
  221.  
  222. ' Display intermediate data
  223. If blnDebug Then
  224. 	For i = 0 To UBound( arrHDD, 2 )
  225. 		WScript.Echo """HDD" & i & """,""" & arrHDD( 0, i ) & """,""" & arrHDD( 1, i ) & """,""" & arrHDD( 2, i ) & """,""" & arrHDD( 3, i ) & """"
  226. 	Next
  227. 	WScript.Echo
  228. 	For i = 0 To UBound( arrCDR, 2 )
  229. 		WScript.Echo """CDROM" & i & """,""" & arrCDR( 0, i ) & """,""" & arrCDR( 1, i ) & """,""" & arrCDR( 2, i ) & """,""" & arrCDR( 3, i ) & """"
  230. 	Next
  231. 	WScript.Echo
  232. 	For i = 0 To UBound( arrHDC, 2 )
  233. 		WScript.Echo """HDC" & i & """,""" & arrHDC( 0, i ) & """,""" & arrHDC( 1, i ) & """,""" & arrHDC( 2, i ) & """"
  234. 	Next
  235. 	WScript.Echo
  236. 	For i = 0 To UBound( arrHost, 2 )
  237. 		WScript.Echo """Host" & i & """,""" & arrHost( 0, i ) & """,""" & arrHost( 1, i ) & """,""" & arrHost( 2, i ) & """"
  238. 	Next
  239. 	WScript.Echo
  240. End If
  241.  
  242. ' Display results
  243. intDescLen = 0
  244. intSizeLen = 0
  245. For i = 0 To UBound( arrHDD, 2 )
  246. 	If Len( arrHDD( 0, i ) ) > intDescLen Then
  247. 		intDescLen = Len( arrHDD( 0, i ) )
  248. 	End If
  249. 	If Len( arrHDD( 2, i ) ) > intSizeLen Then
  250. 		intSizeLen = Len( arrHDD( 2, i ) )
  251. 	End If
  252. Next
  253. For i = 0 To UBound( arrCDR, 2 )
  254. 	If Len( arrCDR( 0, i ) ) > intDescLen Then
  255. 		intDescLen = Len( arrCDR( 0, i ) )
  256. 	End If
  257. Next
  258. For i = 0 To UBound( arrHDD, 2 )
  259. 	If blnAlignOutput Then
  260. 		WScript.Echo "HDD" & i & vbTab & Left( arrHDD( 0, i ) & Space( intDescLen ), intDescLen ) & vbTab & arrHDD( 3, i ) & vbTab & Right( Space( intSizeLen ) & arrHDD( 2, i ), intSizeLen )
  261. 	Else
  262. 		WScript.Echo "HDD" & i & vbTab & arrHDD( 0, i ) & vbTab & arrHDD( 3, i ) & vbTab & arrHDD( 2, i )
  263. 	End If
  264. Next
  265. For i = 0 To UBound( arrCDR, 2 )
  266. 	If blnAlignOutput Then
  267. 		WScript.Echo "CDROM" & i & vbTab & Left( arrCDR( 0, i ) & Space( intDescLen ), intDescLen ) & vbTab & arrCDR( 3, i ) & vbTab & Right( Space( intSizeLen + 3 ) & arrCDR( 2, i ), intSizeLen )
  268. 	Else
  269. 		WScript.Echo "CDROM" & i & vbTab & arrCDR( 0, i ) & vbTab & arrCDR( 3, i ) & vbTab & arrCDR( 2, i )
  270. 	End If
  271. Next
  272.  
  273. Set objItem       = Nothing
  274. Set colItems      = Nothing
  275. Set objWMIService = Nothing
  276.  
  277.  
  278. Sub Syntax
  279. 	strMsg = vbCrLf _
  280. 	       & "DiskTypes.vbs,  Version 1.00 BETA" _
  281. 	       & vbCrLf _
  282. 	       & "List disk drives and their interface type (IDE/SATA/SCSI/USB/USB3) and capacity" _
  283. 	       & vbCrLf & vbCrLf _
  284. 	       & "Usage:  DISKTYPES.VBS  [ /R:computer ]  [ /T ]  [ /D ]" _
  285. 	       & vbCrLf & vbCrLf _
  286. 	       & "Where:  /R:computer    query remote computer" _
  287. 	       & vbCrLf _
  288. 	       & "        /T             generate tab delimited output" _
  289. 	       & vbCrLf _
  290. 	       & "        /D             show intermediate results (for debugging)" _
  291. 	       & vbCrLf & vbCrLf _
  292. 	       & "Notes:  Differentiation between IDE and SATA depends on the words ""SATA"" or" _
  293. 	       & vbCrLf _
  294. 	       & "        ""S-ATA"" or ""Serial ATA"" in the harddisk controller's description field;" _
  295. 	       & vbCrLf _
  296. 	       & "        this is far from fool-proof, but so far the least unreliable method" _
  297. 	       & vbCrLf _
  298. 	       & "        available for scripting." _
  299. 	       & vbCrLf _
  300. 	       & "        Likewise, detection of USB 3.0 depends on the words ""USB 3"" in the USB" _
  301. 	       & vbCrLf _
  302. 	       & "        controller's descrition, or ""NUSB3"" in its PnP Device ID; the latter is" _
  303. 	       & vbCrLf _
  304. 	       & "        true only for NEC/Renesas controllers." _
  305. 	       & vbCrLf _
  306. 	       & "        ""SCSI"" signifies either true SCSI or RAID enabled IDE/SATA controllers." _
  307. 	       & vbCrLf _
  308. 	       & "        ""USB3"" signifies the USB port; the device may still be USB 2.0 or 1.1." _
  309. 	       & vbCrLf & vbCrLf _
  310. 	       & "Written by Rob van der Woude" _
  311. 	       & vbCrLf _
  312. 	       & "http://www.robvanderwoude.com"
  313. 	WScript.Echo strMsg
  314. 	WScript.Quit 1
  315. End Sub
  316.  

page last modified: 2024-04-16; loaded in 0.0087 seconds