Stephon98caffe
Programmer
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
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