the demo script that shows how to use this function is available as a separate download.
复制代码 代码如下:
function readregvalue( mycomputer, myregpath, myregvalue )
' this function 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 first argument)
' readregvalue(1) the hive number (see 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 first element of an
' 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 this in order to find out 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, use 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 the data in an array
if valregtype = reg_binary or valregtype = reg_multi_sz then
' first, deal with registry data which is
' returned as array instead of single value
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, 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.
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论