Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for srvinv.vbs

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

  1. ' Use custom error handling (in this case: none at all)
  2. On Error Resume Next
  3.  
  4. ' Define constants
  5. Const wbemFlagReturnImmediately = &h10
  6. Const wbemFlagForwardOnly       = &h20
  7. Const ForReading                = 1
  8. Const ForWriting                = 2
  9. Const ForAppending              = 8
  10.  
  11. ' Check "named" command line arguments (command line switches)
  12. If WScript.Arguments.Named.Exists( "?" ) Then
  13. 	Syntax
  14. End If
  15. If WScript.Arguments.Named.Exists( "H" ) Then
  16. 	CreateNewLogs
  17. End If
  18. If WScript.Arguments.Named.Exists( "L" ) Then
  19. 	boolLog = True
  20. Else
  21. 	boolLog = False
  22. End If
  23. If WScript.Arguments.Named.Exists( "S" ) Then
  24. 	boolSoftInv = True
  25. Else
  26. 	boolSoftInv = False
  27. End If
  28. If WScript.Arguments.Named.Exists( "Q" ) Then
  29. 	boolQuiet = True
  30. Else
  31. 	boolQuiet = False
  32. End If
  33. If boolQuiet And Not boolLog Then
  34. 	Syntax
  35. End If
  36.  
  37. ' Check "unnamed" command line parameters
  38. Select Case WScript.Arguments.Unnamed.Count
  39. 	Case 0
  40. 		' Default if none specified is local computer (".")
  41. 		Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  42. 		' Retrieve computer name
  43. 		Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  44. 		For Each objItem in colItems
  45. 			strComputer = objItem.Name
  46. 		Next
  47. 	Case 1
  48. 		' Command line parameter can either be a computer
  49. 		' name or "/?" to request online help
  50. 		strComputer = UCase( Wscript.Arguments.Unnamed(0) )
  51. 		If InStr( strComputer, "?" ) > 0 Then
  52. 			Syntax
  53. 		End If
  54. 	Case Else
  55. 		' Maximum is 1 "unnamed" command line parameter
  56. 		Syntax
  57. End Select
  58.  
  59. ' Initialize variables
  60. strLog = strComputer
  61. strMsg = vbCrLf & "Computer name  : " & strComputer & vbCrLf
  62.  
  63. ' Connect to the specified (or default) computer
  64. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
  65.  
  66. ' Retrieve processor info
  67. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  68. For Each objItem In colItems
  69. 	strCPUType = Strip( objItem.Name )
  70. Next
  71.  
  72. ' Retrieve system info
  73. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  74. For Each objItem In colItems
  75. 	If objItem.Domain <> "" Then
  76. 		strLog = strLog & vbTab               & objItem.Domain
  77. 		strMsg = strMsg & "Domain         : " & objItem.Domain     & vbCrLf
  78. 	Else
  79. 		strLog = strLog & vbTab               & objItem.Workgroup
  80. 		strMsg = strMsg & "Domain         : " & objItem.Workgroup  & vbCrLf
  81. 	End If
  82. 	strLog = strLog & vbTab               & objItem.Manufacturer
  83. 	strMsg = strMsg & "Manufacturer   : " & objItem.Manufacturer       & vbCrLf
  84. 	strLog = strLog & vbTab               & objItem.Model
  85. 	strMsg = strMsg & "Model          : " & objItem.Model              & vbCrLf
  86. 	strLog = strLog & vbTab               & objItem.NumberOfProcessors
  87. 	strMsg = strMsg & "Processors     : " & objItem.NumberOfProcessors & vbCrLf
  88. 	strLog = strLog & vbTab               & strCPUType
  89. 	strMsg = strMsg & "Processor type : " & strCPUType                 & vbCrLf
  90. 	strRoles = ""
  91. 	For Each strItem in objItem.Roles
  92. 		strTemp = strItem
  93. 		For Each strTest in "LM_Workstation,LM_Server,NT,Server_NT,Backup_Browser,Potential_Browser,"
  94. 			If strItem = strTest Then
  95. 				strTemp = ""
  96. 			End If
  97. 		Next
  98. 		If strTemp <> "" Then
  99. 			If strRoles = "" Then
  100. 				strRoles = strRoles & ", " & strTemp
  101. 			Else
  102. 				strRoles = strTemp
  103. 			End If
  104. 		End If
  105. 	Next
  106. '	strRoles = Join( objItem.Roles, ", " )
  107. 	strLog = strLog & vbTab               & strRoles
  108. 	strMsg = strMsg & "Roles          : " & strRoles                   & vbCrLf
  109. 	strMemory = Int( ( objItem.TotalPhysicalMemory / 1048576 ) + 0.5 )
  110. 	strLog = strLog & vbTab               & strMemory
  111. 	strMsg = strMsg & "Memory         : " & strMemory         & " MB"  & vbCrLf
  112. Next
  113.  
  114. ' Retrieve OS info
  115. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  116. For Each objItem In colItems
  117. 	strLog = strLog & vbTab               & objItem.Caption
  118. 	strMsg = strMsg & "OS Version     : " & objItem.Caption                 & vbCrLf
  119. 	strLog = strLog & vbTab               & objItem.ServicePackMajorVersion & "." & objItem.ServicePackMinorVersion
  120. 	strMsg = strMsg & "SP Version     : " & objItem.ServicePackMajorVersion & "." & objItem.ServicePackMinorVersion & vbCrLf
  121. 	strLog = strLog & vbTab               & objItem.SerialNumber
  122. 	strMsg = strMsg & "Serial Number  : " & objItem.SerialNumber            & vbCrLf
  123. Next
  124.  
  125. ' Retrieve IP addresses
  126. strIPAddress = ""
  127. strPrevious  = ""
  128. strMsgIP     = ""
  129. strLogIP     = ""
  130. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  131. For Each objItem In colItems
  132. 	strIPAddress = Join( objItem.IPAddress, "," )
  133. 	If strIPAddress <> strPrevious Then
  134. 		If strLogIP = "" Then
  135. 			strLogIP = strIPAddress
  136. 			strMsgIP = "IP Address     : " & strIPAddress
  137. 		Else
  138. 			strLogIP = strLogIP   &   ", " & strIPAddress
  139. 			strMsgIP = "IP Addresses   : " & strLogIP
  140. 		End If
  141. 		strPrevious = strIPAddress
  142. 	End If
  143. Next
  144. strMsg = strMsg & strMsgIP & vbCrLf
  145. strLog = strLog & vbTab & strLogIP
  146.  
  147. ' Retrieve disk info
  148. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_LogicalDisk WHERE MediaType = 12", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  149. strLogDisk = ""
  150. strMsgDisk = ""
  151. For Each objItem In colItems
  152. 	strDisk = objItem.DeviceID & " " & Int( ( objItem.Size / 1073741824 ) + 0.5 )
  153. 	strFree = objItem.DeviceID & " " & Int( ( 100 * objItem.FreeSpace / objItem.Size ) + 0.5 )
  154. 	If strLogDisk = "" Then
  155. 		strLogDisk = strDisk
  156. 		strMsgDisk = strDisk & " GB"
  157. 		strLogFree = strFree
  158. 		strMsgFree = strFree & " %"
  159. 	Else
  160. 		strLogDisk = strLogDisk & ", " & strDisk
  161. 		strMsgDisk = strMsgDisk & ", " & strDisk & " GB"
  162. 		strLogFree = strLogFree & ", " & strFree
  163. 		strMsgFree = strMsgFree & ", " & strFree & " %"
  164. 	End If
  165. Next
  166. strMsg = strMsg & "Logical Drives : " & strMsgDisk & vbCrLf
  167. strLog = strLog & vbTab & strLogDisk
  168. strMsg = strMsg & "Free Space     : " & strMsgFree & vbCrLf
  169. strLog = strLog & vbTab & strLogFree
  170.  
  171. ' Display result unless Quiet mode was specified (/Q switch)
  172. If Not boolQuiet Then
  173. 	WScript.Echo strMsg
  174. End If
  175.  
  176. ' Perform software inventory if specified (/S switch)
  177. If boolSoftInv Then
  178. 	strLogSoft = ListSoftware( )
  179. 	If boolLog Then
  180. 		Set fso = CreateObject( "Scripting.FileSystemObject" )
  181. 		Set filOut = fso.OpenTextFile( "softinv.csv", ForAppending, True ) 
  182. 		filOut.WriteLine( strLogSoft )
  183. 		filOut.Close
  184. 		set fso = Nothing
  185. 	End If
  186. End If
  187.  
  188. 'Append the server inventory to a CSV file if Logging was specified (/L switch)
  189. If boolLog Then
  190. 	Set fso = CreateObject( "Scripting.FileSystemObject" )
  191. 	Set filOut = fso.OpenTextFile( "srvinv.csv", ForAppending, True ) 
  192. 	filOut.WriteLine( strLog )
  193. 	filOut.Close
  194. End If
  195.  
  196. ' End of main program
  197. WScript.Quit( 0 )
  198.  
  199.  
  200.  
  201.  
  202. Sub CreateNewLogs( )
  203. 	' Header line for server inventory log
  204. 	strLog = "Server" & vbTab _
  205. 	       & "Domain" & vbTab _
  206. 	       & "Manufacturer" & vbTab _
  207. 	       & "Model" & vbTab _
  208. 	       & "#CPUs" & vbTab _
  209. 	       & "CPU type" & vbTab _
  210. 	       & "Server roles" & vbTab _
  211. 	       & "Memory (MB)" & vbTab _
  212. 	       & "Windows version" & vbTab _
  213. 	       & "SP" & vbTab _
  214. 	       & "Windows serial number" & vbTab _
  215. 	       & "IP address" & vbTab _
  216. 	       & "Partition size (GB)" & vbTab _
  217. 	       & "Free space"
  218. 	Set fso = CreateObject( "Scripting.FileSystemObject" )
  219. 	Set filOut = fso.OpenTextFile( "srvinv.csv", ForWriting, True )
  220. 	filOut.WriteLine( strLog )
  221. 	filOut.Close
  222.  
  223. 	' Header line for software inventory log
  224. 	strLog = "Server" & vbTab _
  225. 	       & "Software" & vbTab _
  226. 	       & "Version"
  227. '	Set fso = CreateObject( "Scripting.FileSystemObject" )
  228. 	Set filOut = fso.OpenTextFile( "softinv.csv", ForWriting, True )
  229. 	filOut.WriteLine( strLog )
  230. 	filOut.Close
  231. 	set fso = Nothing
  232.  
  233. 	' Abort script
  234. 	WScript.Quit( 0 )
  235. End Sub
  236.  
  237.  
  238. Function ListSoftware( )
  239. 	On Error Resume Next
  240. 	strLogSoft  = ""
  241. 	strMsgSoft  = vbCrLf & "Installed software:" & vbCrLf
  242. 	strPrevious = ""
  243. 	Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
  244. 	Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_ProductSoftwareFeatures", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  245. 	For Each objItem In colItems
  246. 		arrProdInfo = Split( objItem.Product, "," )
  247. 		If strPrevious <> arrProdInfo(1) Then
  248. 			arrProdName = Split( arrProdInfo(1), "=" )
  249. 			strProdName = arrProdName(1)
  250. 			If Left( strProdName, 1 ) = Chr(34) Then
  251. 				strProdName = Mid( strProdName, 2 )
  252. 			End If
  253. 			If Right( strProdName, 1 ) = Chr(34) Then
  254. 				strProdName = Mid( strProdName, 1, Len( strProdName ) - 1 )
  255. 			End If
  256. 			arrProdVer = Split( arrProdInfo(2), "=" )
  257. 			strProdVer = arrProdVer(1)
  258. 			If Left( strProdVer, 1 ) = Chr(34) Then
  259. 				strProdVer = Mid( strProdVer, 2 )
  260. 			End If
  261. 			If Right( strProdVer, 1 ) = Chr(34) Then
  262. 				strProdVer = Mid( strProdVer, 1, Len( strProdVer ) - 1 )
  263. 			End If
  264. 			strMsgSoft = strMsgSoft & strProdName & ", Version " & strProdVer & vbCrLf
  265. 			strLogSoft = strLogSoft & strComputer & vbTab & strProdName & vbTab & strProdVer & vbCrLf
  266. 		End If
  267. 		strPrevious = arrProdInfo(1)
  268. 	Next
  269. 	If Not boolQuiet Then
  270. 		WScript.Echo strMsgSoft
  271. 	End If
  272. 	ListSoftware = strLogSoft
  273. End Function
  274.  
  275.  
  276. Sub ShowError( )
  277. 	strMsg = vbCrLf & "Reference # " & strRef & vbCrLf & "Error # " _
  278. 	       & Err.Number & vbCrLf & Err.Description & vbCrLf & vbCrLf
  279. 	Syntax
  280. End Sub
  281.  
  282.  
  283. Private Function Strip( strInput )
  284. 	Do While Left( strInput, 1 ) = " "
  285. 		strInput = Mid( strInput, 2 )
  286. 	Loop
  287. 	Strip = strInput
  288. End Function
  289.  
  290.  
  291. Sub Syntax( )
  292. 	strMsg = strMsg & vbCrLf _
  293. 	       & "SrvInv.vbs,  Version 1.21" & vbCrLf _
  294. 	       & "Display and log server inventory" & vbCrLf & vbCrLf _
  295. 	       & "Usage:  [ CSCRIPT ]  SRVINV.VBS  [ server_name ]" _
  296. 	       & "  [ /H ]  [ /L ]  [ /Q ]  [ /S ]" & vbCrLf & vbCrLf _
  297. 	       & "Where:  " & Chr(34) & "server_name" & Chr(34) _
  298. 	       & " is the optional name of the server to be probed" & vbCrLf _
  299. 	       & "                      (default is local computer " _
  300. 	       & "name)" & vbCrLf _
  301. 	       & "        /H            create new csv file(s) with Header" & vbCrLf _
  302. 	       & "        /L            Log results in csv file(s)" & vbCrLf _
  303. 	       & "        /Q            Quiet mode, no screen display (requires /L)" & vbCrLf _
  304. 	       & "        /S            perform Software inventory too" & vbCrLf & vbCrLf _
  305. 	       & "Written for Vincent Segers" & vbCrLf _
  306. 	       & "by Rob van der Woude" & vbCrLf _
  307. 	       & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _
  308. 	       & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _
  309. 	       & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _
  310. 	       & "    FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en" & vbCrLf
  311. 	WScript.Echo strMsg
  312. 	WScript.Quit(1)
  313. End Sub
  314.  

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