WMI StdRegProv Implementation code (local or remote) for the registry through WMI operations _vbs

Source: Internet
Author: User
Tags error code error handling goto
Because of its length, only the "code for" function itself is shown on this page.
The demo script that shows the "use of this" function is available as a separate download.
Copy Code code as follows:

Function Readregvalue (MyComputer, Myregpath, Myregvalue)
' This function is reads a value from the registry of any WMI
' Enabled computer.
'
' Arguments:
' MyComputer a computer name or IP address,
' Or a dot for the local computer
' Myregpath a full registry key path, e.g.
' Hkey_classes_root\.jpg or
' Hklm\software\microsoft\directx
' Myregvalue ' the value name to be queried, e.g.
' Installedversion or ' for default
' Values
'
' The function returns an array with the following elements:
' Readregvalue (0) the computer name (the the-the-argument)
' Readregvalue (1) The hive number (for a. Const declarations)
' Readregvalue (2) The key path without the hive
' Readregvalue (3) The Value name (the third argument)
' Readregvalue (4) The error number:0 means no error
' Readregvalue (5) The data type of the result
' Readregvalue (6) The actual data, or the ' a '
' Array of data for REG_BINARY or REG_MULTI_SZ
'
' Written by Rob van der Woude
' Http://www.robvanderwoude.com


' Standard housekeeping
Const HKEY_CLASSES_ROOT = &h80000000
Const HKEY_CURRENT_USER = &h80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &h80000003
Const hkey_current_config = &h80000005
Const hkey_dyn_data = &h80000006 ' Windows 95/98 only

Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const Reg_dword_big_endian = 5
Const Reg_link = 6
Const REG_MULTI_SZ = 7
Const Reg_resource_list = 8
Const Reg_full_resource_descriptor = 9
Const reg_resource_requirements_list = 10
Const Reg_qword = 11

Dim Arrregpath, Arrresult (), arrValueNames, arrValueTypes
Dim I, Objreg, strhive, Valregerror, Valregtype, Valregval

' Assume no error, for now
Valregerror = 0

' Split the registry path in a hive part
' And the rest, and check if that succeeded
Arrregpath = Split (Myregpath, "\", 2)
If IsArray (Arrregpath) Then
If UBound (Arrregpath) <> 1 Then valregerror = 5
Else
Valregerror = 5
End If

' Convert the hive string to a hive number
Select Case UCase (Arrregpath (0))
Case "HKCR", "HKEY_CLASSES_ROOT"
Strhive = HKEY_CLASSES_ROOT
Case "HKCU", "HKEY_CURRENT_USER"
Strhive = HKEY_CURRENT_USER
Case "HKLM", "HKEY_LOCAL_MACHINE"
Strhive = HKEY_LOCAL_MACHINE
Case "HKU", "HKEY_USERS"
Strhive = HKEY_USERS
Case "HKCC", "Hkey_current_config"
Strhive = Hkey_current_config
Case "HKDD", "Hkey_dyn_data"
Strhive = Hkey_dyn_data
Case Else
Valregerror = 5
End Select

' Abort If any error occurred, and return an error code
If valregerror > 0 Then
Readregvalue = Array (MyComputer, Myregpath, _
Myregpath, Myregvalue, _
Valregerror, "-", "-")
Exit Function
End If

' Initiate custom error handling
On Error Resume Next

' Create a WMI Registry object
Set Objreg = GetObject ("winmgmts:{impersonationlevel=impersonate}!//" _
& MyComputer & "/root/default:stdregprov")

' Abort on Failure to create the object
If ERR Then
Valregerror = Err.Number
Err.Clear
On Error Goto 0
Readregvalue = Array (MyComputer, Myregpath, _
Myregpath, Myregvalue, _
Valregerror, "-", "-")
Exit Function
End If

' Get a list of all values in the registry path;
' We need to do ' the
' Exact data type for the requested value
Objreg.enumvalues strhive, Arrregpath (1), arrValueNames, arrValueTypes

' If no values were found, we ' ll need to retrieve a default value
If not IsArray (arrvaluenames) Then
arrValueNames = Array ("")
arrValueTypes = Array (REG_SZ)
End If

If ERR Then
' Abort on failure, returning an error code
Valregerror = Err.Number
Err.Clear
On Error Goto 0
Readregvalue = Array (MyComputer, Myregpath, _
Myregpath, Myregvalue, _
Valregerror, "-", "-")
Exit Function
Else
' Loop through all values in the list ...
For i = 0 to UBound (arrvaluenames)
' ... and find the one requested
If UCase (arrValueNames (i)) = UCase (Myregvalue) Then
' Read the requested value ' s data type
Valregtype = arrValueTypes (i)
' Based on the data type, with the appropriate query to retrieve the data
Select Case Valregtype
Case REG_SZ
Objreg.getstringvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case REG_EXPAND_SZ
Objreg.getexpandedstringvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case REG_Binary ' Returns an array of bytes
Objreg.getbinaryvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case REG_DWORD
Objreg.getdwordvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case REG_MULTI_SZ ' Returns an array of strings
Objreg.getmultistringvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case Reg_qword
Objreg.getqwordvalue strhive, Arrregpath (1), _
Myregvalue, Valregval
If Err Then valregerror = Err.Number
Case Else
Valregerror = 5
End Select
End If
Next
End If

' Check If an error occurred
If valregerror > 0 Then
Valregtype = ""
Valregval = ""
Err.Clear
On Error Goto 0
End If

' Return ' to the data in an array
If Valregtype = reg_binary Or valregtype = REG_MULTI_SZ Then
' deal with registry the data which is
' returned as array instead of single
ReDim Preserve Arrresult (6 + UBound (valregval))
Arrresult (0) = MyComputer
Arrresult (1) = Strhive
Arrresult (2) = Arrregpath (1)
Arrresult (3) = Myregvalue
Arrresult (4) = Valregerror
Arrresult (5) = Valregtype
For i = 0 to UBound (valregval)
Arrresult (6 + i) = Valregval (i)
Next
Readregvalue = Arrresult
Else
Readregvalue = Array (MyComputer, Strhive, Arrregpath (1), _
Myregvalue, Valregerror, Valregtype, Valregval)
End If

' Finished
Set Objreg = Nothing
On Error Goto 0
End Function

Requirements:
Windows Version:me, Watts, XP, Server 2003, or Vista ($, NT 4 with WMI CORE 1.5)
Network:any
Client software:wmi CORE 1.5 for Windows, N or NT 4
Script Engine:any
Summarized:can work on any Windows computer, but WMI CORE 1.5 be required for Windows, or NT 4.
Can is used in *.vbs with CSCRIPT. EXE or WSCRIPT. EXE, as as, as in HTAs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.