13) SetStringValue
爲指定鍵值名稱的鍵值設置鍵值類型爲REG_SZ的鍵值數值。這種方法是最簡單最常用的方法。
uint32 SetStringValue(
[in] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
[in] string sValueName,
[in] string sValue
);
例1:爲鍵值名稱爲StringValue的鍵值賦予類型爲REG_SZ的鍵值數據“www.swynk.com”,該鍵值屬於HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子鍵
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod = "SetStringValue"
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 = "StringValue"
oInParam.sValue = "www.swynk.com"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
Wscript.Echo "String value added successfully"
Else
Wscript.Echo "An error occurred"
End If
例2:本例直接調用SetStringValue。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hDefKey = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"
sValueName = "StringValue"
sValue = "www.swynk.com"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
oOutParam = oRegistry.SetStringValue(hDefKey,sSubKeyName,sValueName,sValue)
WScript.Echo "The return code is : " & oOutParam
例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 = "StringValue"
$sValue = "www.swynk.com"
$oOutParam = $oRegistry.SetStringValue($hDefKey, $sSubKeyName, $sValueName, $sValue)
"Retrun Colde is : " + $oOutParam.ReturnValue
(14) DeleteKey
刪除指定路徑下已存在的子鍵
uint32 DeleteKey(
[in, optional] uint32 hDefKey = 2147483650,
[in] string sSubKeyName
);
例1:刪除HKEY_LOCAL_MACHINE\SOFTWARE路徑下的子鍵“MyCompany”。本例使用ExecMethod_()調用DeleteKey。
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod = "DeleteKey"
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"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
Wscript.Echo "SubKey deleted successfully"
Else
Wscript.Echo "An error occurred"
End If
例2:本例直接調用DeleteKey方法
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hDefKey = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
oOutParam = oRegistry.DeleteKey(hDefKey,sSubKeyName)
WScript.Echo "The return code is : " & oOutParam
例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"
$oOutParam = $oRegistry.DeleteKey($hDefKey, $sSubKeyName)
"Retrun Colde is : " + $oOutParam.ReturnValue
(15) DeleteValue
刪除指定子鍵的指定鍵值名稱的鍵值。
uint32 DeleteValue(
[in, optional] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
[in] string sValueName
);
例1:刪除HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子鍵中鍵值名爲“ToBeDeleted”的鍵值。
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod = "DeleteValue"
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 = "ToBeDeleted"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
Wscript.Echo "Key Value is deleted successfully"
Else
Wscript.Echo "An error occurred"
End If
例2:直接調用DeleteValue方法
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hDefKey = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"
sValueName = "ToBeDeleted"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
oOutParam = oRegistry.DeleteValue(hDefKey,sSubKeyName,sValueName)
WScript.Echo "The return code is : " & oOutParam
例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 = "ToBeDeleted"
$oOutParam = $oRegistry.DeleteKey($hDefKey, $sSubKeyName)
"Retrun Colde is : " + $oOutParam.ReturnValue
(16) CheckAccess
檢查用戶是否具有指定的權限。標準註冊表提供程序的CheckAccess方法只檢查當前帳戶的權限級別,如果當前帳戶沒有適當的權限,該方法返回-1,否則返回0。
uint32 CheckAccess(
[in, optional] uint32 hDefKey = 2147483650,
[in] string sSubKeyName,
[in] uint32 lRequired,
[out] bool bGranted
);
例1:執行下面腳本檢查當前帳戶是否具有“set”權限。
cscript /nologo CheckAccess.vbs set
Const KEY_QUERY_VALUE = &H0001
Const KEY_SET_VALUE = &H0002
Const KEY_CREATE_SUB_KEY = &H0004
Const KEY_ENUMERATE_SUB_KEYS = &H0008
Const KEY_NOTIFY = &H0010
Const KEY_CREATE_LINK = &H0020
Const DELETE = &H00010000
Const READ_CONTROL = &H00020000
Const WRITE_DAC = &H00040000
Const WRITE_OWNER = &H00080000
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
sMethod = "CheckAccess"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\test"
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
sAccess = WScript.Arguments(0)
Select Case sAccess
Case "query" hAccess = KEY_QUERY_VALUE
Case "set" hAccess = KEY_SET_VALUE
Case "createsub" hAccess = KEY_CREATE_SUB_KEY
Case "enumsub" hAccess = KEY_ENUMERATE_SUB_KEYS
Case "notify" hAccess = KEY_NOTIFY
Case "createlink" hAccess = KEY_CREATE_LINK
Case "delete" hAccess = DELETE
Case "readcontrol" hAccess = READ_CONTROL
Case "writedac" hAccess = WRITE_DAC
Case "writeowner" hAccess = WRITE_OWNER
End Select
oInParam.uRequired = hAccess
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
'WScript.Echo oOutParam.Properties_("bGranted")
'If the current user does not have appropriate permissions,the method returns -1
'and 0 otherwise
If oOutParam.Properties_("bGranted") = 0 Then
WScript.Echo "Current user has " & Chr(34) & UCase(sAccess) & Chr(34) & " registry permissions"
Else
WScript.Echo "Current user does not have " & Chr(34) & UCase(sAccess) & chr(34) & " registry permissions"
End If