(07) GetMultiStringValue
返回键值类型为REG_MULTI_SZ的指定键值名称的键值数值
uint32 GetMultiStringValue(
[in, optional] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
[in] string sValueName,
[out] string sValue[]
);
例1:读取HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\System下键值名为Sources的键值数值。这个键值数值是REG_MULTI_SZ 类型的,它是一个以空格相隔的数组。本例直接调用GetMultiStringValue。
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
strValueName = "Sources"
Return = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,_
strValueName,arrValues)
If (Return = 0) And (Err.Number = 0) Then
' Treat the multistring value as a collection of strings separated by spaces and output
For Each strValue In arrValues
WScript.Echo strValue
Next
Else
Wscript.Echo "GetMultiStringValue failed. Error = " & Err.Number
End If
例2:本例使用ExecMethod_()调用GetMultiStringValue。
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod = "GetMultiStringValue"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
strComputer & "/root/default:StdRegProv")
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_LOCAL_MACHINE
oInParam.sSubKeyName = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
oInParam.sValueName = "Sources"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
For iCount = 0 To UBound(oOutParam.Properties_("sValue"))
WScript.Echo oOutParam.Properties_("sValue")(iCount)
Next
Else
Wscript.Echo "GetMultiStringValue failed. Error = " & Err.Number
End If
例3:相应的PS程序
$computer = "."
$namespace = "root\default"
$hDefKey = "&H80000002"
$strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
$strValueName = "Sources"
$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }
$oOutParams = $oRegistry.GetMultiStringValue($hDefKey, $strKeyPath, $strValueName)
If ($oOutParams.returnvalue -eq 0)
{
Foreach($oOutParam in $oOutParams)
{
$oOutParam.sValue
}
}
Else
{
"GetMultiStringValue failed."
}
(08) CreateKey
在指定注册表路径下创建一个子键。
uint32 CreateKey(
[in] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
);
例1:在注册表HKEY_LOCAL_MACHINE\SOFTWARE路径下创建名为MyCompany的子键。此例是直接调用CreateKey。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hDefKey = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
ReturnCode = oRegistry.CreateKey(hDefKey, sSubKeyName)
WScript.Echo ReturnCode
例2:此例是通过ExecMethod_()调用CreateKey方法。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
sMethod = "CreateKey"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\MyCompany"
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
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
例3:相应的PS程序。
$HKEY_LOCAL_MACHINE = "&H80000002"
$computer = "."
$namespace = "root\default"
$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }
$hDefKey = $HKEY_LOCAL_MACHINE
$sSubKeyName = "SOFTWARE\MyCompany-1"
$ReturnCode = $oRegistry.CreateKey($hDefKey, $sSubKeyName)
"Retrun Colde is : " + $ReturnCode.ReturnValue
(09) SetBinaryValue
为指定键值名称的键值设置键值类型为REG_BINARY的键值数值。
uint32 SetBinaryValue(
[in] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
[in] string sValueName,
[in] uint8 uValue[]
);
例1:为键值名称为BinValue的键值赋予类型为REG_BINARY的键值数值“ff 01 01 01”。该键值属于HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子键。
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod = "SetBinaryValue"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
strComputer & "/root/default:StdRegProv")
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_LOCAL_MACHINE
oInParam.sSubKeyName = "SOFTWARE\MyCompany"
oInParam.sValueName = "BinValue"
oInParam.uValue = Array("255","1","1", "1")
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
Wscript.Echo "Binary value added successfully"
Else
Wscript.Echo "An error occurred"
End If
注意uValue是一个字符串数组,每个元素是一个0-255的整数,表示一个单一字节。下图显示正常执行后的结果。
例2:直接调用SetBinaryValue。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\MyCompany"
sValueName = "BinValue"
aValue = Array("255","1","1","1")
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
ReturnCode = oRegistry.SetBinaryValue(hTree, sKey, sValueName, aValue)
If (ReturnCode = 0) And (Err.Number = 0) Then
Wscript.Echo "Binary value added successfully"
Else
Wscript.Echo "An error occurred"
End If
例3:相应的PS程序
$HKEY_LOCAL_MACHINE = "&H80000002"
$computer = "."
$namespace = "root\default"
$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }
$hDefKey = $HKEY_LOCAL_MACHINE
$sSubKeyName = "SOFTWARE\MyCompany"
$sValueName = "BinValue"
$uValue = ("255","1","1", "1")
$ReturnCode = $oRegistry.SetBinaryValue($hDefKey, $sSubKeyName, $sValueName, $uValue)
"Retrun Colde is : " + $ReturnCode.ReturnValue