Link to home
Start Free TrialLog in
Avatar of eclipse-support
eclipse-support

asked on

Delete and Create SNMP registry keys using VBscript

Hi,

Im trying to create a VBscript that will run on our servers at startup and write the SNMP settings to the registry, I can create the keys however i want to be able to delete the previous ones but I keep getting an error saying that the keys cannot be deleted.

Please see my script below and let me know where i have gone wrong -

Dim sKey, FSO, Textfile
'On Error Resume Next

Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")

Set objShell = WScript.CreateObject("WScript.Shell")
Set objNet = CreateObject("WScript.Network")

Set TextFile = FSO.CreateTextFile("C:\Errors\Log.txt")

sKey = "HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters"

TextFile.WriteLine("Registry Keys about to be deleted")

'objshell.RegDelete sKey & "\TrapConfiguration\public\"'

'objshell.RegDelete sKey & "\Permitted Managers"'

objshell.RegDelete sKey & "\ValidCommunities\"

TextFile.WriteLine("Registry Keys about to be written")

objShell.RegWrite sKey & "\TrapConfiguration\communityname\" & "1", "snmphost"

TextFile.WriteLine("1st key written")

objShell.RegWrite sKey & "\ValidCommunities\" & "readme", "communityname"

TextFile.WriteLine("2nd key written")

objShell.RegWrite sKey & "\PermittedManagers\" & "1", "snmphost"

TextFile.WriteLine("3rd key written")

set objUser = Nothing
Set objNet = Nothing
Set objShell = Nothing

wscript.quit

Thanks for any help you can provide.

- Rich
Avatar of WesleySaysHi
WesleySaysHi

I picked up one of the registry keys you want to delete:
HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\public\

Try this code:

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\public\"
oReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
Avatar of eclipse-support

ASKER

Thanks for your help.

I can already delete the "public" key, it's the following keys that i cannot delete -

'objshell.RegDelete sKey & "\Permitted Managers"'

objshell.RegDelete sKey & "\ValidCommunities\"

Thanks,

Rich
ASKER CERTIFIED SOLUTION
Avatar of WesleySaysHi
WesleySaysHi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial