Enumerating Entry Names, Values, and Data Types

Microsoft® Windows® 2000 Scripting Guide

Registry subkeys group entries with related information, and it is often useful to display that related information. Unfortunately, this is not necessarily a straightforward procedure; after all, you cannot read a registry value unless you use the appropriate method. But how can you call the appropriate method if you do not know the data type of the value being read?

Fortunately, you can accomplish this task by using the Registry Provider EnumValues method to retrieve an array containing the entry names and an array containing the data type of each entry. After you know the entry name and its data type, you can select the appropriate method to retrieve and display the value of each entry.

Scripting Steps

Listing 16.13 contains a script that displays the name, data type, and value of all the entries within 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 constants that correspond to the various registry entry data types (lines 2-6).

  3. Create a variable and set it to the computer name (line 8).

  4. Use a GetObject call to connect to the WMI namespace root\default, and set the impersonation level to "impersonate."

  5. Create a variable and set it to the subkey to be enumerated (line 13).

  6. Use the EnumValues method to enumerate the entry names and data types, storing the results in the variables arrEntryNames and arrValueTypes.

  7. Use For Next to loop through each of the entry names stored in the arrEntryNames variable, displaying each entry name and using the corresponding data type in a Select Case statement to display a message identifying the data type (thus determining which method needs to be called to display the value of the entry). For details about each individual Get method, refer to "Reading Entry Values and Types" in this chapter.

Listing 16.13 Enumerating Entries and Data Types

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Lsa"
objReg.EnumValues HKEY_LOCAL_MACHINE,_
 strKeyPath,arrEntryNames,arrValueTypes
 
For i=0 To UBound(arrEntryNames)
 Wscript.Echo "Entry Name: " & arrEntryNames(i)
 Select Case arrValueTypes(i)
 Case REG_SZ
 Wscript.Echo "Data Type: String"
 objReg.GetStringValue HKEY_LOCAL_MACHINE, _
 strKeyPath, arrEntryNames(i),strValue
 Wscript.Echo "Value: " & strValue
 Case REG_EXPAND_SZ
 Wscript.Echo "Data Type: Expanded String"
 objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE _
 strKeyPath, arrEntryNames(i),estrValue
 Wscript.Echo "Value: " & estrValue
 Case REG_BINARY
 Wscript.Echo "Data Type: Binary"
 objReg.GetBinaryValue HKEY_LOCAL_MACHINE, _
 strKeyPath, arrEntryNames(i),arrValue
 WScript.StdOut.Write "Value: "
 For Each byteValue in arrValue
 WScript.StdOut.Write byteValue & " "
 Next
 WScript.StdOut.Write vbCRLF
 Case REG_DWORD
 Wscript.Echo "Data Type: DWORD"
 objReg.GetDWORDValue HKEY_LOCAL_MACHINE, _
 strKeyPath, arrEntryNames(i),dwValue
 Wscript.Echo "Value: " & dwValue
 Case REG_MULTI_SZ
 Wscript.Echo "Data Type: Multi String"
 objReg.GetMultiStringValue HKEY_LOCAL_MACHINE, _
 strKeyPath, arrEntryNames(i),arrValues
 For Each strValue in arrValues
 Wscript.Echo strValue
 Next
 End Select
Next