在root\default命名空间中的StdRegProv类(标准注册表提供程序)提供了下面16种方法,我们将陆续介绍这些方法的使用规则,并给出分别用WBscript和Powershell编写的例子。
GetBinaryValue – 读取BINARY 类型的键值数据
GetDWORDValue – 读取DWORD 类型的键值数据
GetExpandedStringValue – 读取 EXPANDED STRING 类型的键值数据
GetMultiStringValue - 读取MULTI STRING 类型的键值数据。
GetStringValue - 读取STRING 类型的键值数据
CreateKey - 创建子键
SetBinaryValue - 为键值设置 BINARY 类型的键值数据。
SetDWORDValue - 为键值设置DWORD类型键值数据
SetExpandedStringValue – 为键值设置 EXPANDED STRING 类型键值数据
SetMultiStringValue - 为键值设置MULTI STRING 类型键值数据
SetStringValue - 为键值设置STRING类型键值数据
DeleteKey - 删除子键
DeleteValue - 删除键值
EnumKey - 列举子键
EnumValues - 列举键值
CheckAccess - 检查当前帐户权限
下面是在注册表操作时用到有关帐户权限、根键、键值类型等代码。
帐户权限类型:
名称 数值 描述
KEY_QUERY_VALUE &H0001 ability to query registry value
KEY_SET_VALUE &H0002 ability to set registry value
KEY_CREATE_SUB_KEY &H0004 ability to create subkey
KEY_ENUMERATE_SUB_KEYS &H0008 ability to enumerate subke
KEY_NOTIFY &H0010 ability to audit changes to the key
KEY_CREATE_LINK &H0020 ability to create a symbolic link to the key (example of such a link is the one that exists
between HKEY_CLASSES_ROOT and HKEY_LOCAL_MACHINE\SOFTWARE\Classes)
DELETE &H00010000 ability to delete current key
READ_CONTROL &H00020000 ability to read permissions on the current key
WRITE_DAC &H00040000 ability to modify permissions on the current key
WRITE_OWNER &H00080000 ability to take ownership of the current key
根键的代码:
名称 数值
HKEY_CLASSES_ROOT 2147483648, &H80000000
HKEY_CURRENT_USER 2147483649, &H80000001
HKEY_LOCAL_MACHINE 2147483650, &H80000002
HKEY_USERS 2147483651, &H80000003
HKEY_CURRENT_CONFIG 2147483653, &H80000005
HKEY_DYN_DATA 2147483654, &H80000006
键值类型的代码
名称 数值 描述
REG_SZ 1 字符串值
REG_EXPAND_SZ 2 可扩充字符串值
REG_BINARY 3 二进制值
REG_DWORD 4 DWORD值
REG_MULTI_SZ 7 多字符串值
使用wbemtest工具查看\root\default:StdRegProv可以知道指定方法的输入输出参数。例如EnumKey方法有两个输入参数(hDefKey,sSubKeyName)和两个输出参数(ReturnValue,sNames[])。
(01) EnumKey
列举指定路径下的子键(SubKey)。
Uint32 EnumKey(
[in,optional] unit32 hDefKey = 2147483650, (&H80000002,默认)
[in] string sSubKeyName,
[out] string sNames[]
);
例1:列举注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services下的子键名称
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services"
' 输出arrSubKeys 即是sNames[]
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
WScript.Echo "Subkeys under " _
& "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services"
For Each subkey In arrSubKeys
WScript.Echo subkey
Next
例2:通过ExecMethod_()方法调用EnumKey。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
sMethod = "EnumKey"
hTree = HKEY_LOCAL_MACHINE
sKey = "SYSTEM\CurrentControlSet\Services"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
' 创建输入参数
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
' 执行EnumKey方法
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
' 显示执行EnumKey方法的执行状态。一种是读取执行的返回码,另一种是读取输出参数。
WScript.Echo "The return code of ExecMethod EnumKey is: " & oOutParam.ReturnValue
WScript.Echo "The return code of ExecMethod EnumKey is: " & oOutParam.Properties_("ReturnValue")
' 另一个输出参数是数组sNames[],显示子键的名称。
For i=0 To UBound(oOutParam.Properties_("sNames"))
WScript.Echo oOutParam.Properties_("sNames")(i)
Next
从例1和例2中可以知道,通过SWbemObject访问CIM对象和类的属性和方法,可以使用下面两种方法之一访问低层CIM对象的属性和方法:
直接调用StdRegProv类的方法时(如例1),只需要使用它的原名称执行方法或属性来获取数据,好象它就是一个SWbemObject属性或方法。
使用SWbemServices.ExecMethod、SWbemObject.ExecMothod_、SWbemObject.ExecMothodAsync_ 间接调用StdRegProv类的方法时(如例2),需要通过Properties_集合来获取数据。
例3:相应的Powershell程序。因为是直接使用EnumKey方法,通过输出参数获取数据。在PS中EnumKey方法格式有所不同:
EnumKey(System.UInt32 hDefKey, System.string sSubKeyName)
$computer = "."
$namespace = "root\Default"
$HKLM = "&H80000002" # 也可以写成 $HKLM = 2147483650
$strKeyPath = "SYSTEM\CurrentControlSet\Services"
$oreg = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }
$arrSubKeys = $oreg.EnumKey($HKLM, $strKeyPath)
"Subkeys under " + "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services"
ForEach($subkey In $arrSubKeys)
{
"Return code is : " + $subkey.returnvalue
$subkey.sNames
}