Member_2_7980832
asked on
Novice question on VBScript
Hello folks,
I'm working on a writing a VBScript and I have no experience with VBS. I can do this with a command line with no problem, however, it needs to be in VBS.
The script I'm working on checks to see if 2 registry key exist, and if so, deletes a third key. I've been looking around on the net and found some code that works for what I want to do. My issue is on the delete RegKey code. I found this code which works fine, however, if I modify it to point to a different location, it doesn't work.
Here is the code I found and if I put a key in the registry, it deletes it just fine: HKCU\Software\Test
But if I change the code to this, it doesn't work. What am I doing wrong? HKLM\Software\Test
I'm working on a writing a VBScript and I have no experience with VBS. I can do this with a command line with no problem, however, it needs to be in VBS.
The script I'm working on checks to see if 2 registry key exist, and if so, deletes a third key. I've been looking around on the net and found some code that works for what I want to do. My issue is on the delete RegKey code. I found this code which works fine, however, if I modify it to point to a different location, it doesn't work.
Here is the code I found and if I put a key in the registry, it deletes it just fine: HKCU\Software\Test
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
strKeyPath = "Software\Test"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_CURRENT_USER, strKeypath
Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
But if I change the code to this, it doesn't work. What am I doing wrong? HKLM\Software\Test
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath = "Software\Test"
Set objRegistry = GetObject("winmgmts:\\" &
strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath
Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub
ASKER
I'm using MetaPad and verified that it's a DOS VBS file. The only thing I changed is the first line:
Const HKEY_CURRENT_USER = &H80000001
to
Const HKEY_LOCAL_MACHINE = &H80000002
and all instances of HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE. The first one works fine, but the second one doesn't.
Looks like I also have a typo in the second script. In line 6 I forgot the underscore, but it's there in the script I have.
Const HKEY_CURRENT_USER = &H80000001
to
Const HKEY_LOCAL_MACHINE = &H80000002
and all instances of HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE. The first one works fine, but the second one doesn't.
Looks like I also have a typo in the second script. In line 6 I forgot the underscore, but it's there in the script I have.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, so I've tried to use this to elevate the permissions, but it's STILL not working. My script name is Axcess-regkey.VBS and it's in the same directory.
Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\Axcess-regkey.VBS" & Chr(34), "", "runas", 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Not sure what I was doing wrong, but it seems to be working now. Thanks for the help folks.
What line is the error on...? You can put on error resume which may make it work.