Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hardware.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Use variables locally
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check if FINDSTR and WMIC are available
  9. FINDSTR.EXE /? >NUL 2>&1 || GOTO Syntax
  10. WMIC.EXE    /? >NUL 2>&1 || GOTO Syntax
  11.  
  12. :: Set initial and default values
  13. SET _40_Spaces=                                        % 40 spaces %
  14. SET idxCDROM=0
  15. SET idxCPU=0
  16. SET idxHDD=0
  17. SET idxNIC=0
  18. SET idxVDU=0
  19. SET Node=%ComputerName%
  20.  
  21. :: Check command line argument
  22. IF NOT "%~2"=="" GOTO Syntax
  23. ECHO.%* | FINDSTR.EXE /R /C:"[/?]" >NUL && GOTO Syntax
  24. IF "%~1"=="" (SET Node=%ComputerName%) ELSE (SET Node=%~1)
  25.  
  26. :: Check validity of node name
  27. ECHO.%Node%| FINDSTR.EXE /R /C:"[/?*,;:/\\&<>+ ]" >NUL && GOTO Syntax
  28. PING %Node% -n 2 2>NUL | FIND "TTL=" >NUL || GOTO Syntax
  29.  
  30. :: Initialize log file
  31. FOR /F "skip=2 tokens=2-7 delims=," %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:csv') DO (
  32. 	SET Today=%%F%%D%%A
  33. 	SET Now=%%B%%C%%E
  34. )
  35. SET Log=Hardware_%Today%_%Now%.csv
  36.  
  37. IF NOT "%Debug%"=="" (
  38. 	SET Log
  39. )
  40.  
  41. :: Create a header with the computer name
  42. CALL :CreateLine "Host" "Name=%ComputerName%"
  43. SET csvHostName=%Value%
  44. SET hdrHostName=%csvProperty%
  45.  
  46. IF NOT "%Debug%"=="" (
  47. 	SET csvHostName
  48. 	SET hdrHostName
  49. )
  50.  
  51. :: Run inventory; Component Subroutine's 4th argument may be modified to change the detail level; See comments in Component Subroutine
  52. CALL :Component "BaseBoard"                 "BaseBoard"     "Manufacturer Product Version"                                        "0"
  53. CALL :Chassis
  54. CALL :Component "CPU"                       "CPU"           "Name CurrentClockSpeed Manufacturer MaxClockSpeed SocketDesignation" "2"
  55. CALL :Component "Win32_PhysicalMemoryArray" "Memory"        "MemoryDevices"                                                       "0" "Banks"
  56. CALL :Component "Win32_PhysicalMemory"      "Memory Module" "Capacity"                                                            "3"
  57. CALL :Component "Win32_PhysicalMemory"      "Memory Module" "Speed"                                                               "4"
  58. CALL :HardDisk
  59. CALL :CDROM
  60. CALL :Component "Win32_VideoController"     "VideoCard"     "AdapterRAM Name VideoModeDescription"                                "2"
  61. CALL :NIC
  62. CALL :Ports
  63. CALL :Slots
  64. CALL :Component "BIOS"                      "BIOS"          "Manufacturer Name ReleaseDate Version"                               "0"
  65.  
  66. :: Concatenate text and write to CSV file
  67. SET csvLine=%Today%,%Now%,%csvHostName%%csvBaseBoard%%csvChassis%%csvCPU%%csvWin32_PhysicalMemoryArray%%csvWin32_PhysicalMemory%%csvHardDisk%%csvCDROM%%csvWin32_VideoController%%csvNIC%%csvPorts%%csvSlots%%csvBIOS%
  68. SET hdrLine=Date,Time,%hdrHostName%%hdrBaseBoard%%hdrChassis%%hdrCPU%%hdrWin32_PhysicalMemoryArray%%hdrWin32_PhysicalMemory%%hdrHardDisk%%hdrCDROM%%hdrWin32_VideoController%%hdrNIC%%hdrPorts%%hdrSlots%%hdrBIOS%
  69.  
  70. >  %Log% ECHO.%hdrLine%
  71. >> %Log% ECHO.%csvLine%
  72.  
  73. IF NOT "%Debug%"=="" (
  74. 	SET csvLine
  75. 	SET hdrLine
  76. )
  77.  
  78. :: Done
  79. ENDLOCAL
  80. GOTO:EOF
  81.  
  82.  
  83. :: * :: * :: * :: * :: Subroutines :: * :: * :: * :: * ::
  84.  
  85.  
  86. :CDROM
  87. FOR /F "skip=2 tokens=2,3 delims=," %%A IN ('WMIC /Node:%Node% CDROM Get DeviceId^,Name /Format:csv') DO (
  88. 	CALL :CreateLine "CD-ROM #!idxCDROM!" "Name=%%~B"
  89. 	CALL :GetLength "%%~B"
  90. 	SET /A Length = !Length! + 5
  91. 	FOR /F "tokens=1,2 delims=\" %%C IN ("%%~A") DO (
  92. 		SET FwVer=%%~D
  93. 		CALL SET FwVer=%%FwVer:~^!Length^!%%
  94. 		SET FwVer=!FwVer:_=!
  95. 		CALL :CreateLine "CD-ROM #!idxCDROM!" "InterfaceType=%%~C"
  96. 		CALL :CreateLine "CD-ROM #!idxCDROM!" "FirmwareVersion=!FwVer!"
  97. 		SET csvCDROM=!csvCDROM!,%%~B,%%~C,!FWVer!
  98. 		SET hdrCDROM=!hdrCDROM!,CDROM#!idxCDROM! Name,CDROM#!idxCDROM! Interface,CDROM#!idxCDROM! FirmwareVersion
  99. 	)
  100. 	SET /A idxCDROM = idxCDROM + 1
  101. )
  102. CALL :CreateLine "CD-ROM" "Count=%idxCDROM%"
  103. SET csvCDROM=%csvCDROM%,%Value%
  104. SET hdrCDROM=%hdrCDROM%,%csvProperty%
  105. IF NOT "%Debug%"=="" (
  106. 	SET csvCDROM
  107. 	SET hdrCDROM
  108. )
  109. GOTO:EOF
  110.  
  111.  
  112. :Chassis
  113. :: Based on Chassis.vbs, a VBScript to interogate a machine's
  114. :: chassis type, by Guy Thomas http://computerperformance.co.uk/
  115. FOR /F "tokens=2 delims=={}" %%A IN ('WMIC /Node:%Node% SystemEnclosure Get ChassisTypes /format:list') DO (
  116. 	IF %%A EQU  1 SET csvChassis=,Maybe Virtual Machine
  117. 	IF %%A EQU  2 SET csvChassis=,??
  118. 	IF %%A EQU  3 SET csvChassis=,Desktop
  119. 	IF %%A EQU  4 SET csvChassis=,Thin Desktop
  120. 	IF %%A EQU  5 SET csvChassis=,Pizza Box
  121. 	IF %%A EQU  6 SET csvChassis=,Mini Tower
  122. 	IF %%A EQU  7 SET csvChassis=,Full Tower
  123. 	IF %%A EQU  8 SET csvChassis=,Portable
  124. 	IF %%A EQU  9 SET csvChassis=,Laptop
  125. 	IF %%A EQU 10 SET csvChassis=,Notebook
  126. 	IF %%A EQU 11 SET csvChassis=,Hand Held
  127. 	IF %%A EQU 12 SET csvChassis=,Docking Station
  128. 	IF %%A EQU 13 SET csvChassis=,All in One
  129. 	IF %%A EQU 14 SET csvChassis=,Sub Notebook
  130. 	IF %%A EQU 15 SET csvChassis=,Space-Saving
  131. 	IF %%A EQU 16 SET csvChassis=,Lunch Box
  132. 	IF %%A EQU 17 SET csvChassis=,Main System Chassis
  133. 	IF %%A EQU 18 SET csvChassis=,Lunch Box
  134. 	IF %%A EQU 19 SET csvChassis=,SubChassis
  135. 	IF %%A EQU 20 SET csvChassis=,Bus Expansion Chassis
  136. 	IF %%A EQU 21 SET csvChassis=,Peripheral Chassis
  137. 	IF %%A EQU 22 SET csvChassis=,Storage Chassis
  138. 	IF %%A EQU 23 SET csvChassis=,Rack Mount Unit
  139. 	IF %%A EQU 24 SET csvChassis=,Sealed-Case PC
  140. )
  141. SET hdrChassis=,Chassis Type
  142. CALL :CreateLine "Chassis" "Type=%csvChassis:,=%
  143. IF NOT "%Debug%"=="" (
  144. 	SET csvChassis
  145. 	SET hdrChassis
  146. )
  147. GOTO:EOF
  148.  
  149.  
  150. :Component
  151. :: %1 = "Component Name or Alias", e.g. "BIOS", "HDD", "Win32_PhysicalMemoryArray"
  152. :: %2 = "Component Friendly Name", e.g. "BIOS", "Harddisk", "Memory"
  153. :: %3 = "Alphabetized Properties List", e.g. "Manufacturer Product Version"
  154. :: %4 = "Multiple Instances", 0=No, 1=List, 2=Count Number, 3=Count Value, 4=Count Number But Don't Show Count, e.g.  "0" for BaseBoard, BIOS, "1" for CD-ROM, Harddisk, Memory Modules, "2" for CPU, Memory Banks, "3" for Memory Capacity
  155. :: %5 = "Property Alias", use only on single property queries, will replace the property name
  156. SET Alias=%~1
  157. IF /I "%Alias:~0,6%"=="Win32_" SET Alias=Path %Alias%
  158. SET Properties=%~3
  159. IF "%~4"=="0" (
  160. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC.EXE /Node:%Node% %Alias% Get %Properties: =^^^,% /Format:List ^| FIND.EXE "="') DO (
  161. 		IF "%~5"=="" (
  162. 			CALL :CreateLine "%~2" "%%~A=%%~B"
  163. 		) ELSE (
  164. 			CALL :CreateLine "%~2" "%~5=%%~B"
  165. 		)
  166. 		CALL SET csv%~1=!csv%~1!,!Value!
  167. 		CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  168. 	)
  169. )
  170. IF "%~4"=="1" (
  171. 	SET idxInstance=0
  172. 	FOR %%A IN (%Properties%) DO SET LastProperty=%%A
  173. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC.EXE /Node:%Node% %Alias% Get %Properties: =^^^,% /Format:List ^| FIND.EXE "="') DO (
  174. 		CALL :CreateLine "%~2 #!idxInstance!" "%%~A=%%~B"
  175. 		CALL SET csv%~1=!csv%~1!,!Value!
  176. 		CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  177. 		IF "%%~A"=="!LastProperty!" SET /A idxInstance = !idxInstance! + 1
  178. 	)
  179. 	CALL :CreateLine "%~2" "Count=!idxInstance!"
  180. 	CALL SET csv%~1=!csv%~1!,!Value!
  181. 	CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  182. )
  183. IF "%~4"=="2" (
  184. 	SET idxInstance=0
  185. 	FOR %%A IN (%Properties%) DO SET LastProperty=%%A
  186. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC.EXE /Node:%Node% %Alias% Get %Properties: =^^^,% /Format:List ^| FIND.EXE "="') DO (
  187. 		IF "!idxInstance!"=="0" (
  188. 			CALL :CreateLine "%~2" "%%~A=%%~B"
  189. 			CALL SET csv%~1=!csv%~1!,!Value!
  190. 			CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  191. 		)
  192. 		IF "%%~A"=="!LastProperty!" SET /A idxInstance = !idxInstance! + 1
  193. 	)
  194. 	CALL :CreateLine "%~2" "Count=!idxInstance!"
  195. 	CALL SET csv%~1=!csv%~1!,!Value!
  196. 	CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  197. )
  198. IF "%~4"=="3" (
  199. 	SET idxInstance=0
  200. 	SET TotalCount=0
  201. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC.EXE /Node:%Node% %Alias% Get %Properties: =^^^,% /Format:List ^| FIND.EXE "="') DO (
  202. 		CALL SET /A TotalCount = !TotalCount! + %%~B
  203. 		SET /A idxInstance = !idxInstance! + 1
  204. 	)
  205. 	CALL :CreateLine "%~2" "Capacity=!TotalCount!"
  206. 	CALL :CreateLine "%~2" "Count=!idxInstance!"
  207. 	CALL SET csv%~1=!csv%~1!,!Value!
  208. 	CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  209. )
  210. IF "%~4"=="4" (
  211. 	SET idxInstance=0
  212. 	FOR %%A IN (%Properties%) DO SET LastProperty=%%A
  213. 	FOR /F "tokens=1* delims==" %%A IN ('WMIC.EXE /Node:%Node% %Alias% Get %Properties: =^^^,% /Format:List ^| FIND.EXE "="') DO (
  214. 		IF "!idxInstance!"=="0" (
  215. 			CALL :CreateLine "%~2" "%%~A=%%~B"
  216. 			CALL SET csv%~1=!csv%~1!,!Value!
  217. 			CALL SET hdr%~1=!hdr%~1!,!csvProperty!
  218. 		)
  219. 		IF "%%~A"=="!LastProperty!" SET /A idxInstance = !idxInstance! + 1
  220. 	)
  221. )
  222. IF NOT "%Debug%"=="" (
  223. 	CALL SET csv%~1
  224. 	CALL SET hdr%~1
  225. )
  226. GOTO:EOF
  227.  
  228.  
  229. :CreateLine
  230. FOR /F "tokens=1* delims==" %%a IN ("%~2") DO (
  231. 	SET csvProperty=%~1 %%~a
  232. 	SET Property=%~1 %%~a%_40_Spaces%
  233. 	SET Value=%%~b
  234. )
  235. ECHO.%Property:~0,40%= %Value%
  236. GOTO:EOF
  237.  
  238.  
  239. :GetLength
  240. SET Var=%~1
  241. SET Length=0
  242. FOR /L %%Z IN (80,-1,0) DO (
  243. 	IF "!Var:~%%Z,1!"=="" SET Length=%%Z
  244. )
  245. SET Var=
  246. GOTO:EOF
  247.  
  248.  
  249. :HardDisk
  250. FOR /F "tokens=*" %%A IN ('WMIC DiskDrive Where "SCSITargetID Is Not Null" Get Index^,InterfaceType^,Model^,Size /Format:list ^| FIND "="') DO (
  251. 	ECHO.%%A | FIND /I "Index=" >NUL
  252. 	IF ERRORLEVEL 1 (
  253. 		CALL :CreateLine "HardDisk #!Index!" "%%~A"
  254. 		CALL SET csvHardDisk=!csvHardDisk!,!Value!
  255. 		CALL SET hdrHardDisk=!hdrHardDisk!,!csvProperty!
  256. 	) ELSE (
  257. 		SET %%A
  258. 	)
  259. )
  260. SET /A Index = %Index% + 1
  261. CALL :CreateLine "HardDisk" "Count=%Index%"
  262. SET csvHardDisk=%csvHardDisk%,%Value%
  263. SET hdrHardDisk=%hdrHardDisk%,%csvProperty%
  264. IF NOT "%Debug%"=="" (
  265. 	SET csvHardDisk
  266. 	SET hdrHardDisk
  267. )
  268. GOTO:EOF
  269.  
  270.  
  271. :NIC
  272. FOR /F "tokens=2-5 delims=," %%A IN ('WMIC /Node:%Node% Path Win32_NetworkAdapter Get AdapterType^,MACAddress^,Manufacturer^,ProductName /Format:csv ^| FIND /I ",Ethernet" ^| FIND /I /V ",Microsoft,"') DO (
  273. 	CALL :CreateLine "NIC #!idxNIC!" "AdapterType=%%~A"
  274. 	CALL SET csvNIC=!csvNIC!,!Value!
  275. 	CALL SET hdrNIC=!hdrNIC!,!csvProperty!
  276. 	CALL :CreateLine "NIC #!idxNIC!" "MACAddress=%%~B"
  277. 	CALL SET csvNIC=!csvNIC!,!Value!
  278. 	CALL SET hdrNIC=!hdrNIC!,!csvProperty!
  279. 	CALL :CreateLine "NIC #!idxNIC!" "Manufacturer=%%~C"
  280. 	CALL SET csvNIC=!csvNIC!,!Value!
  281. 	CALL SET hdrNIC=!hdrNIC!,!csvProperty!
  282. 	CALL :CreateLine "NIC #!idxNIC!" "ProductName=%%~D"
  283. 	CALL SET csvNIC=!csvNIC!,!Value!
  284. 	CALL SET hdrNIC=!hdrNIC!,!csvProperty!
  285. 	FOR /F "tokens=3 delims=," %%E IN ('WMIC /Node:%Node% /Namespace:"\\root\WMI" Path MSNdis_LinkSpeed Where Active^=TRUE Get InstanceName^,NdisLinkSpeed /Format:csv ^| FIND ",%%~D,"') DO (
  286. 		CALL :CreateLine "NIC #!idxNIC!" "NdisLinkSpeed=%%~E"
  287. 		CALL SET csvNIC=!csvNIC!,!Value!
  288. 		CALL SET hdrNIC=!hdrNIC!,!csvProperty!
  289. 	)
  290. 	SET /A idxNIC = !idxNIC! + 1
  291. )
  292. CALL :CreateLine "NIC" "Count=%idxNIC%"
  293. CALL SET csvNIC=%csvNIC%,%Value%
  294. CALL SET hdrNIC=%hdrNIC%,%csvProperty%
  295. IF NOT "%Debug%"=="" (
  296. 	SET csvNIC
  297. 	SET hdrNIC
  298. )
  299. GOTO:EOF
  300.  
  301.  
  302.  
  303. :Ports
  304. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_USBController Get Name /Format:list 2^>NUL ^| FIND /C "="') DO CALL :CreateLine "USB"      "Ports=%%A"
  305. SET csvPorts=,%Value%
  306. SET hdrPorts=,%csvProperty%
  307. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_ParallelPort  Get Name /Format:list 2^>NUL ^| FIND /C "="') DO CALL :CreateLine "Parallel" "Ports=%%A"
  308. SET csvPorts=%csvPorts%,%Value%
  309. SET hdrPorts=%hdrPorts%,%csvProperty%
  310. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_SerialPort    Get Name /Format:list 2^>NUL ^| FIND /C "="') DO CALL :CreateLine "Serial"   "Ports=%%A"
  311. SET csvPorts=%csvPorts%,%Value%
  312. SET hdrPorts=%hdrPorts%,%csvProperty%
  313. IF NOT "%Debug%"=="" (
  314. 	SET csvPorts
  315. 	SET hdrPorts
  316. )
  317. GOTO:EOF
  318.  
  319.  
  320. :Slots
  321. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_SystemSlot Get SlotDesignation /Format:List 2^>NUL ^| FINDSTR /R /I /C:"=AGP" ^| FIND /C "="') DO SET AGPSlots=%%A
  322. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_SystemSlot Get SlotDesignation /Format:List 2^>NUL ^| FINDSTR /R /I /C:"=PCI" ^| FIND /C "="') DO SET PCISlots=%%A
  323. FOR /F %%A IN ('WMIC /Node:%Node% Path Win32_SystemSlot Get SlotDesignation /Format:List 2^>NUL ^| FIND /C "="')                            DO SET AllSlots=%%A
  324. SET /A OtherSlots = %AllSlots% - %AGPSlots% - %PCISlots%
  325. CALL :CreateLine "AGP Slot" "Count=%AGPSlots%"
  326. SET csvSlots=,%Value%
  327. SET hdrSlots=,%csvProperty%
  328. CALL :CreateLine "PCI Slot" "Count=%PCISlots%"
  329. SET csvSlots=%csvSlots%,%Value%
  330. SET hdrSlots=%hdrSlots%,%csvProperty%
  331. CALL :CreateLine "Other Slots" "Count=%PCISlots%"
  332. SET csvSlots=%csvSlots%,%Value%
  333. SET hdrSlots=%hdrSlots%,%csvProperty%
  334. IF NOT "%Debug%"=="" (
  335. 	SET csvSlots
  336. 	SET hdrSlots
  337. )
  338. GOTO:EOF
  339.  
  340.  
  341. :Syntax
  342. ECHO.
  343. ECHO Hardware.bat, Version 3.10 for Windows XP Professional and later
  344. ECHO Display hardware summary for any WMI enabled computer on the network
  345. ECHO.
  346. ECHO Usage:  HARDWARE    [ computer ]
  347. ECHO.
  348. ECHO Where:  "computer"  is the computer to be checked (default is local system)
  349. ECHO.
  350. ECHO Notes:  Requires WMIC, which is native in Windows XP Professional and
  351. ECHO         Windows Server 2003.
  352. ECHO         The results are displayed on screen in list format, and written
  353. ECHO         to a "time stamped" CSV file named Hardware_yyyyMMdd_hhmmss.csv.
  354. ECHO         To debug, SET environment variable DEBUG to anything and run again.
  355. ECHO.
  356. ECHO Written by Rob van der Woude
  357. ECHO http://www.robvanderwoude.com
  358. IF "%OS%"=="Windows_NT" ENDLOCAL
  359. EXIT /B 1
  360.  

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