Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wmi.reg.vbs

(view source code of wmi.reg.vbs as plain text)

  1. ' This script is just a demo for the function it contains: ReadRegValue()
  2. ' ReadRegValue() will read a value from the registry on any computer, and
  3. ' without the need to specify the data type for the registry value.
  4. ' This data type is returned by ReadRegValue(), besides the data itself.
  5. ' Read the comment lines inside the function for more details.
  6.  
  7. Option Explicit
  8.  
  9. Dim arrRegTypes, arrRegVal, i, strComputer, strMsg, strTemp
  10.  
  11. arrRegTypes = Array( "", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", _
  12.                      "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", _
  13.                      "REG_MULTI_SZ", "REG_RESOURCE_LIST", _
  14.                      "REG_FULL_RESOURCE_DESCRIPTOR", _
  15.                      "REG_RESOURCE_REQUIREMENTS_LIST", "REG_QWORD" )
  16.  
  17. ' Specify a computer name, or use a dot for the local computer
  18. strComputer = "."
  19.  
  20. ' Let's read the default file type associated with .PDF extensions
  21. arrRegVal = ReadRegValue( strComputer, "HKEY_CLASSES_ROOT\.pdf", "" )
  22. If arrRegVal( 4 ) = 0 Then
  23. 	strTemp = arrRegVal( 6 )
  24. 	strMsg = "[HKEY_CLASSES_ROOT\.pdf]" & vbCrLf _
  25. 	       & "@=" & arrRegVal( 6 ) & vbCrLf
  26. 	If UBound( arrRegVal ) > 6 Then
  27. 		For i = 5 To UBound( arrRegVal )
  28. 			strMsg = strMsg & Space( 12 ) _
  29. 			       & arrRegVal( i ) & vbCrLf
  30. 		Next
  31. 	End If
  32. 	strMsg = strMsg & "Data type: " _
  33. 	       & arrRegTypes( arrRegVal( 5 ) ) & vbCrLf
  34. Else
  35. 	On Error Resume Next
  36. 	Err.Raise arrRegVal( 4 )
  37. 	strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
  38. 	Err.Clear
  39. 	On Error Goto 0
  40. End If
  41. WScript.Echo strMsg
  42.  
  43. ' Now let's use the result from the previous invocation to
  44. ' read the default (open) command associated with .PDF files
  45. arrRegVal = ReadRegValue( strComputer, "HKEY_CLASSES_ROOT\" _
  46.                         & strTemp & "\shell\open\command", "" )
  47. If arrRegVal( 4 ) = 0 Then
  48. 	strMsg = "[HKEY_CLASSES_ROOT\" & strTemp _
  49. 	       & "\shell\open\command]" & vbCrLf _
  50. 	       & "@=" & arrRegVal( 6 ) & vbCrLf
  51. 	If UBound( arrRegVal ) > 6 Then
  52. 		For i = 6 To UBound( arrRegVal )
  53. 			strMsg = strMsg & Space( 12 ) _
  54. 			       & arrRegVal( i ) & vbCrLf
  55. 		Next
  56. 	End If
  57. 	strMsg = strMsg & "Data type: " _
  58. 	       & arrRegTypes( arrRegVal( 5 ) ) & vbCrLf
  59. Else
  60. 	On Error Resume Next
  61. 	Err.Raise arrRegVal( 4 )
  62. 	strMsg = "Error: " & Err.Number _
  63. 	       & vbCrLf & Err.Description
  64. 	Err.Clear
  65. 	On Error Goto 0
  66. End If
  67. WScript.Echo strMsg
  68.  
  69. ' And this code demonstrates reading REG_BINARY data,
  70. ' which is returned as an array instead of a single value
  71. arrRegVal = ReadRegValue( strComputer, _
  72.                           "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectX", _
  73.                           "InstalledVersion" )
  74. If arrRegVal( 4 ) = 0 Then
  75. 	strMsg = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectX]" & vbCrLf _
  76. 	       & "InstalledVersion=" 
  77. 	strMsg = strMsg & arrRegVal( 6 )
  78. 	strMsg = strMsg & vbCrLf
  79. 	If UBound( arrRegVal ) > 6 Then
  80. 		For i = 6 To UBound( arrRegVal )
  81. 			strMsg = strMsg & Space( 17 ) _
  82. 			       & arrRegVal( i ) & vbCrLf
  83. 		Next
  84. 	End If
  85. 	strMsg = strMsg & "Data type: " _
  86. 	       & arrRegTypes( arrRegVal( 5 ) ) & vbCrLf
  87. Else
  88. 	On Error Resume Next
  89. 	Err.Raise arrRegVal( 4 )
  90. 	strMsg = "Error: " & Err.Number _
  91. 	       & vbCrLf & Err.Description
  92. 	Err.Clear
  93. 	On Error Goto 0
  94. End If
  95. WScript.Echo strMsg
  96.  
  97.  
  98.  
  99.  
  100.  
  101. Function ReadRegValue( myComputer, myRegPath, myRegValue )
  102. ' This function reads a value from the registry of any WMI
  103. ' enabled computer.
  104. '
  105. ' Arguments:
  106. ' myComputer        a computer name or IP address,
  107. '                   or a dot for the local computer
  108. ' myRegPath         a full registry key path, e.g.
  109. '                   HKEY_CLASSES_ROOT\.jpg or
  110. '                   HKLM\SOFTWARE\Microsoft\DirectX
  111. ' myRegValue        the value name to be queried, e.g.
  112. '                   InstalledVersion or "" for default
  113. '                   values
  114. '
  115. ' The function returns an array with the following elements:
  116. ' ReadRegValue(0)   the computer name (the first argument)
  117. ' ReadRegValue(1)   the hive number (see const declarations)
  118. ' ReadRegValue(2)   the key path without the hive
  119. ' ReadRegValue(3)   the value name (the third argument)
  120. ' ReadRegValue(4)   the error number: 0 means no error
  121. ' ReadRegValue(5)   the data type of the result
  122. ' ReadRegValue(6)   the actual data, or the first element of an
  123. '                   array of data for REG_BINARY or REG_MULTI_SZ
  124. '
  125. ' Written by Rob van der Woude
  126. ' http://www.robvanderwoude.com
  127.  
  128.  
  129. 	' Standard housekeeping
  130. 	Const HKEY_CLASSES_ROOT              = &H80000000
  131. 	Const HKEY_CURRENT_USER              = &H80000001
  132. 	Const HKEY_LOCAL_MACHINE             = &H80000002
  133. 	Const HKEY_USERS                     = &H80000003
  134. 	Const HKEY_CURRENT_CONFIG            = &H80000005
  135. 	Const HKEY_DYN_DATA                  = &H80000006 ' Windows 95/98 only
  136.  
  137. 	Const REG_SZ                         =  1
  138. 	Const REG_EXPAND_SZ                  =  2
  139. 	Const REG_BINARY                     =  3
  140. 	Const REG_DWORD                      =  4
  141. 	Const REG_DWORD_BIG_ENDIAN           =  5
  142. 	Const REG_LINK                       =  6
  143. 	Const REG_MULTI_SZ                   =  7
  144. 	Const REG_RESOURCE_LIST              =  8
  145. 	Const REG_FULL_RESOURCE_DESCRIPTOR   =  9
  146. 	Const REG_RESOURCE_REQUIREMENTS_LIST = 10
  147. 	Const REG_QWORD                      = 11
  148.  
  149. 	Dim arrRegPath, arrResult(), arrValueNames, arrValueTypes
  150. 	Dim i, objReg, strHive, valRegError, valRegType, valRegVal
  151.  
  152. 	' Assume no error, for now
  153. 	valRegError = 0
  154.  
  155. 	' Split the registry path in a hive part
  156. 	' and the rest, and check if that succeeded
  157. 	arrRegPath = Split( myRegPath, "\", 2 )
  158. 	If IsArray( arrRegPath ) Then
  159. 		If UBound( arrRegPath ) <> 1 Then valRegError = 5
  160. 	Else
  161. 		valRegError = 5
  162. 	End If
  163.  
  164. 	' Convert the hive string to a hive number
  165. 	Select Case UCase( arrRegPath( 0 ) )
  166. 		Case "HKCR", "HKEY_CLASSES_ROOT"
  167. 			strHive = HKEY_CLASSES_ROOT
  168. 		Case "HKCU", "HKEY_CURRENT_USER"
  169. 			strHive = HKEY_CURRENT_USER
  170. 		Case "HKLM", "HKEY_LOCAL_MACHINE"
  171. 			strHive = HKEY_LOCAL_MACHINE
  172. 		Case "HKU",  "HKEY_USERS"
  173. 			strHive = HKEY_USERS
  174. 		Case "HKCC", "HKEY_CURRENT_CONFIG"
  175. 			strHive = HKEY_CURRENT_CONFIG
  176. 		Case "HKDD", "HKEY_DYN_DATA"
  177. 			strHive = HKEY_DYN_DATA
  178. 		Case Else
  179. 			valRegError = 5
  180. 	End Select
  181.  
  182. 	' Abort if any error occurred, and return an error code
  183. 	If valRegError > 0 Then
  184. 		ReadRegValue = Array( myComputer, myRegPath, _
  185. 		                      myRegPath, myRegValue, _
  186. 		                      valRegError, "-", "-" )
  187. 		Exit Function
  188. 	End If
  189.  
  190. 	' Initiate custom error handling
  191. 	On Error Resume Next
  192.  
  193. 	' Create a WMI registry object
  194. 	Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" _
  195. 	           & myComputer & "/root/default:StdRegProv" )
  196.  
  197. 	' Abort on failure to create the object
  198. 	If Err Then
  199. 		valRegError = Err.Number
  200. 		Err.Clear
  201. 		On Error Goto 0
  202. 		ReadRegValue = Array( myComputer, myRegPath, _
  203. 		                      myRegPath, myRegValue, _
  204. 		                      valRegError, "-", "-" )
  205. 		Exit Function
  206. 	End If
  207.  
  208. 	' Get a list of all values in the registry path;
  209. 	' we need to do this in order to find out the
  210. 	' exact data type for the requested value
  211. 	objReg.EnumValues strHive, arrRegPath( 1 ), arrValueNames, arrValueTypes
  212.  
  213. 	' If no values were found, we'll need to retrieve a default value
  214. 	If Not IsArray( arrValueNames ) Then
  215. 		arrValueNames = Array( "" )
  216. 		arrValueTypes = Array( REG_SZ )
  217. 	End If
  218.  
  219. 	If Err Then
  220. 		' Abort on failure, returning an error code
  221. 		valRegError = Err.Number
  222. 		Err.Clear
  223. 		On Error Goto 0
  224. 		ReadRegValue = Array( myComputer, myRegPath, _
  225. 		                      myRegPath, myRegValue, _
  226. 		                      valRegError, "-", "-" )
  227. 		Exit Function
  228. 	Else
  229. 		' Loop through all values in the list . . .
  230. 		For i = 0 To UBound( arrValueNames )
  231. 			' . . . and find the one requested
  232. 			If UCase( arrValueNames( i ) ) = UCase( myRegValue ) Then
  233. 				' Read the requested value's data type
  234. 				valRegType = arrValueTypes( i )
  235. 				' Based on the data type, use the appropriate query to retrieve the data
  236. 				Select Case valRegType
  237. 					Case REG_SZ
  238. 						objReg.GetStringValue strHive, arrRegPath( 1 ), _
  239. 						                      myRegValue, valRegVal
  240. 						If Err Then valRegError = Err.Number
  241. 					Case REG_EXPAND_SZ
  242. 						objReg.GetExpandedStringValue strHive, arrRegPath( 1 ), _
  243. 						                              myRegValue, valRegVal
  244. 						If Err Then valRegError = Err.Number
  245. 					Case REG_BINARY ' returns an array of bytes
  246. 						objReg.GetBinaryValue strHive, arrRegPath( 1 ), _
  247. 						                      myRegValue, valRegVal
  248. 						If Err Then valRegError = Err.Number
  249. 					Case REG_DWORD
  250. 						objReg.GetDWORDValue strHive, arrRegPath( 1 ), _
  251. 						                     myRegValue, valRegVal
  252. 						If Err Then valRegError = Err.Number
  253. 					Case REG_MULTI_SZ ' returns an array of strings
  254. 						objReg.GetMultiStringValue strHive, arrRegPath( 1 ), _
  255. 						                           myRegValue, valRegVal
  256. 						If Err Then valRegError = Err.Number
  257. 					Case REG_QWORD
  258. 						objReg.GetQWORDValue strHive, arrRegPath( 1 ), _
  259. 						                     myRegValue, valRegVal
  260. 						If Err Then valRegError = Err.Number
  261. 					Case Else
  262. 						valRegError = 5
  263. 				End Select
  264. 			End If
  265. 		Next
  266. 	End If
  267.  
  268. 	' Check if an error occurred
  269. 	If valRegError > 0 Then
  270. 		valRegType = ""
  271. 		valRegVal  = ""
  272. 		Err.Clear
  273. 		On Error Goto 0
  274. 	End If
  275.  
  276. 	' Return the data in an array
  277. 	If valRegType = REG_BINARY Or valRegType = REG_MULTI_SZ Then
  278. 		' First, deal with registry data which is
  279. 		' returned as array instead of single value
  280. 		ReDim Preserve arrResult( 6 + UBound( valRegVal ) )
  281. 		arrResult( 0 ) = myComputer
  282. 		arrResult( 1 ) = strHive
  283. 		arrResult( 2 ) = arrRegPath( 1 )
  284. 		arrResult( 3 ) = myRegValue
  285. 		arrResult( 4 ) = valRegError
  286. 		arrResult( 5 ) = valRegType
  287. 		For i = 0 To UBound( valRegVal )
  288. 			arrResult( 6 + i ) = valRegVal( i )
  289. 		Next
  290. 		ReadRegValue = arrResult
  291. 	Else
  292. 		ReadRegValue = Array( myComputer, strHive, arrRegPath( 1 ), _
  293. 		                      myRegValue, valRegError, valRegType, valRegVal )
  294. 	End If
  295.  
  296. 	' Finished
  297. 	Set objReg = Nothing
  298. 	On Error Goto 0
  299. End Function
  300.  

page last uploaded: 2017-08-21, 14:26