Creating String-valued and DWORD-valued Entries

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The Registry Provider s SetStringValue and SetDWORDValue methods enable you to create string and DWORD-valued registry entries. Each of the methods accepts the same four parameters: a constant corresponding to the subtree in which the entry will be created, the path to the subkey in which the entry will be created, the name of the entry, and the value of the entry.

Note that the subkey must already exist. If the subkey does not exist, the script cannot create it for you. Because no location is available for the entry, the script will fail.

Be careful to use the method that corresponds to the data type of the value you want to create. If you want to create an entry with the DWORD value 8 and you mistakenly use the SetStringValue method and pass it the value 8, the method will interpret the 8 as the string "8" without warning you. This could incorrectly configure the registration, and lead to any number of problems..

Scripting Steps

Listing 16.9 contains a script that creates two entries in the System Admin Scripting Guide subkey, one having a string value and the other a DWORD value, in the subkey created by the script in Listing 16.8. Before running this script, run the script in Listing 16.8 to create the subkey SOFTWARE\System Admin Scripting Guide.

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 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 variables and set them to the following:
    • Subkey in which the entries will be created
    • Name of the string-valued registry entry being created
    • String value of the registry entry being created
  5. Use the SetStringValue method to create the new string value.
  6. Create variables and set them to the following:
    • Name of the DWORD-valued registry entry being created
    • DWORD value of the registry entry being created
  7. Use the SetDWORDValue method to create the new DWORD value.

Listing 16.9   Creating Registry String-valued and DWORD-valued Entries

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _      strComputer & "\root\default:StdRegProv") strKeyPath = "SOFTWARE\System Admin Scripting Guide" strEntryName = "String Value Name" strValue = "string value" objReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strEntryName,strValue strEntryName = "DWORD Value Name" dwValue = 82 objReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strEntryName,dwValue

Figure 16.5 shows the newly created string and DWORD-valued entries in a registry editor.

Figure 16.5   Viewing Newly Created Entries Using a Registry Editor

Viewing Newly Created Entries Using a Registry Editor


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