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.


代码如下:

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.

(0)

相关推荐

  • 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. 复制代码 代码如下: Function ReadRegValue( myComputer, myRegPath, myRegValue ) ' This

  • vb.net操作注册表的方法分析【增加,修改,删除,查询】

    本文实例讲述了vb.net操作注册表的方法.分享给大家供大家参考,具体如下: 增加: Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software/Faxturer", True) Dim subkey As Microsoft.Win32.RegistryKey subkey = key.CreateSubKey("Manu")

  • 从命令行快速操作注册表(图)

    每一个熟悉Windows的人都相信,通过注册表几乎可以做到任何事情,真正的问题是怎样迅速找到正确的注册键. 绝大多数情形下,操作注册表意味着找到某个特定的子键:偶尔地,可能需要修改一下子键的名称(例如,可能要进行结构上的改动).那么,怎样才能迅速找到正确的子键并执行修改?另外,如果你不仅要改动本地机器的注册表,而且还要对远程机器作同样的修改,该怎么办?本文告诉你一个好工具Regfind.Regfind可以从Windows 2000 Server的Resource Kit Supplement O

  • C#操作注册表的方法详解

    本文实例讲述了C#操作注册表的方法.分享给大家供大家参考,具体如下: 下面我们就来用.NET下托管语言C#注册表操作,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和删除.判断注册表项是否存在.判断键值是否存在. 准备工作: 1. 要操作注册表,我们必须要引入必要的命名空间: 复制代码 代码如下: using Microsoft.Win32; 在这个命名空间里面包含了许多注册表相关的类,足够我们使用了~~ 2. 命名空间里面提供了一个类:RegistryKey 利用它

  • 批处理操作注册表完全攻略(读取注册表/写入注册表等)

    一,批处理生成.Reg文件操作注册表 用批处理中的重定向符号可以轻松地生成.reg文件.然后用命令执行.reg文件即可! 这里,着重要了解.reg文件操作注册表的方法. 首先.reg文件首行必须是:Windows Registry Editor Version 5.00.然后才是操作注册表的内容. (就和从注册表中导出的文件格式一致) 1,创建子项 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\TTT] 在HK

  • 用RegFind从命令行快速操作注册表

    每一个熟悉Windows的人都相信,通过注册表几乎可以做到任何事情,真正的问题是怎样迅速找到正确的注册键. 绝大多数情形下,操作注册表意味着找到某个特定的子键:偶尔地,可能需要修改一下子键的名称(例如,可能要进行结构上的改动).那么,怎样才能迅速找到正确的子键并执行修改?另外,如果你不仅要改动本地机器的注册表,而且还要对远程机器作同样的修改,该怎么办?本文告诉你一个好工具Regfind.Regfind可以从Windows 2000 Server的Resource Kit Supplement O

  • 易语言操作注册表的细节分享

    关键是了解易语言自带的: "写注册项" 这个方法. 如: 写注册项 (#本地机器, "SOFTWARE\Microsoft\Internet Explorer\MAIN\Start Page", "http://www.baidu.com") 这样就在#本地机器中的指定位置键下写入了默认键值信息. 那么第一个参数"#本地机器"代表什么意思呢? 看下图就知道了 以上是易语言和注册表中对应键关系. 具体操作注册表,再上一图片就明白

  • Python模块 _winreg操作注册表

    用python操作修改windows注册表,显然要比用C或者C++简单. 主要参考资料:官方文档:https://docs.python.org/zh-cn/3/library/winreg.html 通过python操作注册表主要有两种方式,一种是通过python的内置模块 _winreg,另一种方式就是 Win32 Extension For Python的win32api模块.这里主要简单看看用内置模块 _winreg如何操作注册表. 1.读取 读取用的方法是OpenKey方法:打开特定的

  • python3操作注册表的方法(Url protocol)

    使用python操作注册表的方法最近学习了一下,现在做一下笔记,由于对Python语言的使用还不是很熟练,所以写不出高大上的结构,但是解决问题搓搓有余了. 说道注册表顺便也说一说环境变量的修改(放下下一节讲),里面有许多相似的地方,我觉得放在一起说会比较合适. 操作注册表 操作注册表主要引入winreg包,winreg包是python3自带的包,python2的名字和python3有区别_winreg,在这里只关注python3 key = winreg.OpenKeyEx(winreg.HKE

  • Python操作注册表详细步骤介绍

    Python操作注册表步骤之1.打开注册表 对注册表进行操作前,必须打开注册表.在Python中,可以使用以下两个函数:RegOpenKey和RegOpenKeyEx.其函数原型分别如下所示. RegOpenKey(key, subKey , reserved , sam)RegOpenKeyEx (key, subKey , reserved , sam) 两个函数的参数一样.参数含义如下: key:必须为表10-1中列出的项. subKey:要打开的子项. reserved:必须为0. sa

随机推荐