Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing permission of a registry

Status
Not open for further replies.

Stephon98caffe

Programmer
Feb 18, 2005
25
US
I would like to delete the contents of a registry using a script. I think my logic is correct but it won't delete contents. For some reason this statement Return = objReg3.DeleteKey(HKU, strKeyPath3) does not return 0. I also think there is a problem with permission on the object that won't allow me to delete it.


On Error Resume Next
Const HKU = &H80000003

strComputer = "."
Set StdOut = WScript.StdOut

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg2=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg3=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg4=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg5=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")


strKeyPath = ""
oReg.EnumKey HKU, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
' .DEFAULT, etc.
' Check for the contents of HKEY_USERS->Software
strKeyPath2 = subkey & "\Software"
oReg2.EnumKey HKU, strKeyPath2, arrSubKeys2
For Each subkey2 In arrSubKeys2
If subkey2 = "CENS Client" Then
' Nelda said that if the "CENS Client" hive exists, then assume the Settings sub-hive exists
strKeyPath3 = strKeyPath2 & "\CENS Client\Settings"

'Delete the ...\CENS Client\Settings hive
Return = objReg3.DeleteKey(HKU, strKeyPath3)
If (Return = 0) And (Err.Number = 0) Then
'Wscript.Echo "HKEY_USERS\..\Software\CENS Client\Settings successfully deleted"
Else
Wscript.Echo "Could not delete" & strKeyPath3 & " hive. Error = " & Err.Number
End If

'Recreate the ...\CENS Client\Settings hive
Return2 = objReg4.CreateKey(HKU, strKeyPath3)

If (Return2 = 0) And (Err.Number = 0) Then
'Wscript.Echo "HKEY_LOCAL_MACHINE\Software\MyKey\MySubKey created"
Else
Wscript.Echo "Could not recreate" & strKeyPath3 & " hive. Error = " & Err.Number
End If

'Add proper CENS values to hive
strKeyPath = strKeyPath3
strValueName = "Groups"
strValue = ""
strValueName2 = "HDR1"
strValue2 = " strValueName3 = "HDR2"
strValue3 = " strValueName4 = "URL1"
strValue4 = " strValueName5 = "URL2"
strValue5 = "
' write string value to key
Return = objReg5.SetStringValue(HKU,strKeyPath,strValueName,strValue)
If (Return = 0) And (Err.Number = 0) Then
Else
Wscript.Echo "SetStringValue failed. Error = " & Err.Number
End If
Return = objReg5.SetStringValue(HKU,strKeyPath,strValueName2,strValue2)
If (Return = 0) And (Err.Number = 0) Then
Else
Wscript.Echo "SetStringValue failed. Error = " & Err.Number
End If
Return = objReg5.SetStringValue(HKU,strKeyPath,strValueName3,strValue3)
If (Return = 0) And (Err.Number = 0) Then
Else
Wscript.Echo "SetStringValue failed. Error = " & Err.Number
End If
Return = objReg5.SetStringValue(HKU,strKeyPath,strValueName4,strValue4)
If (Return = 0) And (Err.Number = 0) Then
Else
Wscript.Echo "SetStringValue failed. Error = " & Err.Number
End If
Return = objReg5.SetStringValue(HKU,strKeyPath,strValueName5,strValue5)
If (Return = 0) And (Err.Number = 0) Then
Else
Wscript.Echo "SetStringValue failed. Error = " & Err.Number
End If
End if
Next
Next
 
i would say one ref to WMI would be enough...you dont need all of these

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg2=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg3=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg4=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

Set oReg5=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
not even sure if you need wmi for this might be more readable and easier is you just use WshShell.RegRead, REgDelete, you look like you are only writing REG_SZ values.
if you wanted to stck with WMI this might be ok


oReg.EnumKey HKU, "Software\CENS Client", arrSubKeys

For Each aSubKey In arrSubKeys
If LCase(aSubKey) = "settings" Then
'delete then add again
End If
Next

if not then

WshShell.RegDelete "hkcu\software\cens client\settings"
WshShell.RegWrite ""hkcu\software\cens client\settings\hello", "world", REG_SZ

or something like that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top