Enumerating Subkeys of a Key or Subkey

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

In some instances, useful configuration information is stored as the names of a set of subkeys. In the following sample script, for instance, the subkey names represent the services on a computer. In a case such as this, simply listing the names of the subkeys provides useful information.

The EnumKey method enables you to return the subkeys of a registry key or subkey. Note that the EnumKey method returns only the immediate subkeys of a key or subkey; it does not return any subkeys that might be contained within those top-level subkeys. For example, suppose you run the EnumKey method against the Control subkey shown in Figure 16.7. In this case, EnumKey will return an array consisting of the subkeys AGP, Arbiters, and BackupRestore. However, that array will not include the subkeys of those subkeys (such as AllocationOrder and DllPaths).

Figure 16.7   Sample Registry Subkeys

Sample Registry Subkeys

To return the subkeys of Arbiters and BackupRestore, you need to use a recursive function. For more information about recursive functions, see "VBScript Primer" in this book.

Scripting Steps

Listing 16.12 contains a script that enumerates the subkeys of a registry subkey. To carry out this task, the script must perform the following steps:

  1. Create a constant that holds the hexadecimal number corresponding to the HKEY_LOCAL_MACHINE subtree.
  2. Create a variable and set it to the computer name.
  3. Use a GetObject call to connect to the WMI namespace root\default, and set the impersonation level to "impersonate."
  4. Create a variable and set it to the subkey to be enumerated.
  5. Use the EnumKey method to enumerate the subkeys, storing the results in the arrSubKeys variable.
  6. For each of the subkey names stored in the array arrSubKey, use the Echo method to display the values.

Listing 16.12   Enumerating Subkeys of a Subkey

1 2 3 4 5 6 7 8 9 10 11 12 
Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _      strComputer & "\root\default:StdRegProv") strKeyPath = "SYSTEM\CurrentControlSet\Services" objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys For Each Subkey in arrSubKeys     Wscript.Echo Subkey Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net