In Visual Basic Script, how do you Enumerate all the subkeys of a given registry key when it is a 64 bit key?
I have some visual basic script code to recursively delete registry keys. It works fine on the 32bit keys (under HKEY_LOCAL_MACHINE\Software\Wow6432Node) but does not execute under the 64 bit keys (at HKEY_LOCAL_MACHINE\Software). What do I need to make to the code to get this to execute properly for both 32 and 64 bit keys? I've worked on this for a while scouring the internet and making several approaches. Hopefully someone out there has the solution.
Begin Code:
'----------------------------------------------------------
Sub DeleteSubkeys(Key, strKeyPath)
Dim arrSubkeys, strSubkey
objRegistry.EnumKey Key, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys Key, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey Key, strKeyPath
End Sub
'----------------------------------------------------------
End of Code
BTW - I did run across this code at ITNinjas while I was looking for the 64 bit solution so the general problem has been worked on before.
Answers (2)
Call the 64 bit vbs exe file not the 32. If you are using kace to call the script the client is 32 bit so it calls the 32bit vbs exe file (cscript or wscript) those files exist as 32 and 64 bit executables.
On a x64 call the exe using the sysnative command vs standard path variables.
path:
c:\windows\sysnative
executable
Cscript.exe
parameters
runthis.vbs
I did find the answer. Change the 64 to a 32 when referring to 32 bit keys. Detailed code below:
Option Explicit
Dim oCtx, oLocator, oReg, Return, arrSubKeys, strSubKey
Const HKEY_LOCAL_MACHINE = &H80000002
DeleteSubkeys HKEY_LOCAL_MACHINE, "Software\Mykey", 64
Sub DeleteSubkeys (Key, strKeyPath, RegType)
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", RegType
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
oReg.EnumKey Key, strKeyPath, arrSubKeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys Key, strKeyPath & "\" & strSubkey, RegType
Next
End If
Return = oReg.DeleteKey(Key, strKeyPath)
End Sub