批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[转贴] VBScript脚本StdRegProv通过WMI操作注册表

WMI StdRegProv 通过wmi操作注册表的vbscript实现代码 (本地或远程),需要的朋友可以参考下。

Because of its length, only the code for the function itself is shown on this page.
The demo script that shows how to use this function is available as a separate download.

  1. Function ReadRegValue( myComputer, myRegPath, myRegValue )
  2. ' This function reads a value from the registry of any WMI
  3. ' enabled computer.
  4. '
  5. ' Arguments:
  6. ' myComputer a computer name or IP address,
  7. ' or a dot for the local computer
  8. ' myRegPath a full registry key path, e.g.
  9. ' HKEY_CLASSES_ROOT\.jpg or
  10. ' HKLM\SOFTWARE\Microsoft\DirectX
  11. ' myRegValue the value name to be queried, e.g.
  12. ' InstalledVersion or "" for default
  13. ' values
  14. '
  15. ' The function returns an array with the following elements:
  16. ' ReadRegValue(0) the computer name (the first argument)
  17. ' ReadRegValue(1) the hive number (see const declarations)
  18. ' ReadRegValue(2) the key path without the hive
  19. ' ReadRegValue(3) the value name (the third argument)
  20. ' ReadRegValue(4) the error number: 0 means no error
  21. ' ReadRegValue(5) the data type of the result
  22. ' ReadRegValue(6) the actual data, or the first element of an
  23. ' array of data for REG_BINARY or REG_MULTI_SZ
  24. '
  25. ' Written by Rob van der Woude
  26. ' http://www.robvanderwoude.com
  27. ' Standard housekeeping
  28. Const HKEY_CLASSES_ROOT = &H80000000
  29. Const HKEY_CURRENT_USER = &H80000001
  30. Const HKEY_LOCAL_MACHINE = &H80000002
  31. Const HKEY_USERS = &H80000003
  32. Const HKEY_CURRENT_CONFIG = &H80000005
  33. Const HKEY_DYN_DATA = &H80000006 ' Windows 95/98 only
  34. Const REG_SZ = 1
  35. Const REG_EXPAND_SZ = 2
  36. Const REG_BINARY = 3
  37. Const REG_DWORD = 4
  38. Const REG_DWORD_BIG_ENDIAN = 5
  39. Const REG_LINK = 6
  40. Const REG_MULTI_SZ = 7
  41. Const REG_RESOURCE_LIST = 8
  42. Const REG_FULL_RESOURCE_DESCRIPTOR = 9
  43. Const REG_RESOURCE_REQUIREMENTS_LIST = 10
  44. Const REG_QWORD = 11
  45. Dim arrRegPath, arrResult(), arrValueNames, arrValueTypes
  46. Dim i, objReg, strHive, valRegError, valRegType, valRegVal
  47. ' Assume no error, for now
  48. valRegError = 0
  49. ' Split the registry path in a hive part
  50. ' and the rest, and check if that succeeded
  51. arrRegPath = Split( myRegPath, "\", 2 )
  52. If IsArray( arrRegPath ) Then
  53. If UBound( arrRegPath ) <> 1 Then valRegError = 5
  54. Else
  55. valRegError = 5
  56. End If
  57. ' Convert the hive string to a hive number
  58. Select Case UCase( arrRegPath( 0 ) )
  59. Case "HKCR", "HKEY_CLASSES_ROOT"
  60. strHive = HKEY_CLASSES_ROOT
  61. Case "HKCU", "HKEY_CURRENT_USER"
  62. strHive = HKEY_CURRENT_USER
  63. Case "HKLM", "HKEY_LOCAL_MACHINE"
  64. strHive = HKEY_LOCAL_MACHINE
  65. Case "HKU", "HKEY_USERS"
  66. strHive = HKEY_USERS
  67. Case "HKCC", "HKEY_CURRENT_CONFIG"
  68. strHive = HKEY_CURRENT_CONFIG
  69. Case "HKDD", "HKEY_DYN_DATA"
  70. strHive = HKEY_DYN_DATA
  71. Case Else
  72. valRegError = 5
  73. End Select
  74. ' Abort if any error occurred, and return an error code
  75. If valRegError > 0 Then
  76. ReadRegValue = Array( myComputer, myRegPath, _
  77. myRegPath, myRegValue, _
  78. valRegError, "-", "-" )
  79. Exit Function
  80. End If
  81. ' Initiate custom error handling
  82. On Error Resume Next
  83. ' Create a WMI registry object
  84. Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" _
  85. & myComputer & "/root/default:StdRegProv" )
  86. ' Abort on failure to create the object
  87. If Err Then
  88. valRegError = Err.Number
  89. Err.Clear
  90. On Error Goto 0
  91. ReadRegValue = Array( myComputer, myRegPath, _
  92. myRegPath, myRegValue, _
  93. valRegError, "-", "-" )
  94. Exit Function
  95. End If
  96. ' Get a list of all values in the registry path;
  97. ' we need to do this in order to find out the
  98. ' exact data type for the requested value
  99. objReg.EnumValues strHive, arrRegPath( 1 ), arrValueNames, arrValueTypes
  100. ' If no values were found, we'll need to retrieve a default value
  101. If Not IsArray( arrValueNames ) Then
  102. arrValueNames = Array( "" )
  103. arrValueTypes = Array( REG_SZ )
  104. End If
  105. If Err Then
  106. ' Abort on failure, returning an error code
  107. valRegError = Err.Number
  108. Err.Clear
  109. On Error Goto 0
  110. ReadRegValue = Array( myComputer, myRegPath, _
  111. myRegPath, myRegValue, _
  112. valRegError, "-", "-" )
  113. Exit Function
  114. Else
  115. ' Loop through all values in the list . . .
  116. For i = 0 To UBound( arrValueNames )
  117. ' . . . and find the one requested
  118. If UCase( arrValueNames( i ) ) = UCase( myRegValue ) Then
  119. ' Read the requested value's data type
  120. valRegType = arrValueTypes( i )
  121. ' Based on the data type, use the appropriate query to retrieve the data
  122. Select Case valRegType
  123. Case REG_SZ
  124. objReg.GetStringValue strHive, arrRegPath( 1 ), _
  125. myRegValue, valRegVal
  126. If Err Then valRegError = Err.Number
  127. Case REG_EXPAND_SZ
  128. objReg.GetExpandedStringValue strHive, arrRegPath( 1 ), _
  129. myRegValue, valRegVal
  130. If Err Then valRegError = Err.Number
  131. Case REG_BINARY ' returns an array of bytes
  132. objReg.GetBinaryValue strHive, arrRegPath( 1 ), _
  133. myRegValue, valRegVal
  134. If Err Then valRegError = Err.Number
  135. Case REG_DWORD
  136. objReg.GetDWORDValue strHive, arrRegPath( 1 ), _
  137. myRegValue, valRegVal
  138. If Err Then valRegError = Err.Number
  139. Case REG_MULTI_SZ ' returns an array of strings
  140. objReg.GetMultiStringValue strHive, arrRegPath( 1 ), _
  141. myRegValue, valRegVal
  142. If Err Then valRegError = Err.Number
  143. Case REG_QWORD
  144. objReg.GetQWORDValue strHive, arrRegPath( 1 ), _
  145. myRegValue, valRegVal
  146. If Err Then valRegError = Err.Number
  147. Case Else
  148. valRegError = 5
  149. End Select
  150. End If
  151. Next
  152. End If
  153. ' Check if an error occurred
  154. If valRegError > 0 Then
  155. valRegType = ""
  156. valRegVal = ""
  157. Err.Clear
  158. On Error Goto 0
  159. End If
  160. ' Return the data in an array
  161. If valRegType = REG_BINARY Or valRegType = REG_MULTI_SZ Then
  162. ' First, deal with registry data which is
  163. ' returned as array instead of single value
  164. ReDim Preserve arrResult( 6 + UBound( valRegVal ) )
  165. arrResult( 0 ) = myComputer
  166. arrResult( 1 ) = strHive
  167. arrResult( 2 ) = arrRegPath( 1 )
  168. arrResult( 3 ) = myRegValue
  169. arrResult( 4 ) = valRegError
  170. arrResult( 5 ) = valRegType
  171. For i = 0 To UBound( valRegVal )
  172. arrResult( 6 + i ) = valRegVal( i )
  173. Next
  174. ReadRegValue = arrResult
  175. Else
  176. ReadRegValue = Array( myComputer, strHive, arrRegPath( 1 ), _
  177. myRegValue, valRegError, valRegType, valRegVal )
  178. End If
  179. ' Finished
  180. Set objReg = Nothing
  181. On Error Goto 0
  182. End Function
复制代码


Requirements:
Windows version: ME, 2000, XP, Server 2003, or Vista (95, 98, NT 4 with WMI CORE 1.5)
Network: any
Client software: WMI CORE 1.5 for Windows 95, 98 or NT 4
Script Engine: any
Summarized: Can work on any Windows computer, but WMI CORE 1.5 is required for Windows 95, 98 or NT 4.
Can be used in *.vbs with CSCRIPT.EXE or WSCRIPT.EXE, as well as in HTAs.

http://www.jb51.net/article/29245.htm

返回列表