Recipe9.2.Setting a Value


Recipe 9.2. Setting a Value

Problem

You want to create, modify, or delete a registry value.

Solution

Using a graphical user interface

  1. Open the Registry Editor (regedit.exe).

  2. Browse to the parent key of the value you want to set or delete.

  3. To create a value:

  4. Right-click on the parent key and select New and the type of value you want to create.

  5. Type the name of the value and hit enter twice. This should cause the Edit dialog box to open.

  6. Type the value for the value and click OK.

  7. To modify a value:

    1. In the right pane, right-click on the value and select Modify.

    2. Enter the new data for the value and click OK.

  8. To delete a value:

    1. In the right pane, right-click on the value and select Delete.

    2. Click Yes to confirm.

Using a command-line interface

The following command sets a registry value:

> reg add \\<ServerName>\<Key> /v <ValueName> /t <ValueType> /d <ValueData>

For example:

> reg add \\fs01\HKLM\Software\Rallencorp /v Version /t REG_SZ /d "1.2" > reg add \\.\HKLM\Software\Rallencorp /v Setting1 /t REG_DWORD /d 1024

One nice thing about the reg add command is that it automatically creates the Rallencorp subkey if it doesn't already exist.

This command deletes a registry value:

> reg delete \\<ServerName>\<Key> /v <ValueName>

For example:

> reg delete \\fs01\HKLM\Software\Rallencorp /v Version

Using VBScript

WMI has different methods for setting each of the registry value datatypes. For example, to set a DWORD, you must use SetDWORDValue, not SetStringValue. See Table 9-1 for the complete list of methods.


' This code sets a registry string value ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"             ' e.g., Software\Rallencorp strStringValueName = "<ValueName>"  ' e.g., Version strStringValue = "<ValueData>"      ' e.g., 1.2 strComputer = "<ServerName>"        ' e.g., server01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.SetStringValue(HKLM, strKeyPath, strStringValueName, _                               strStringValue) if intRC <> 0 then    WScript.Echo "Error setting value: " & intRC else    WScript.Echo "Successfully set value: " & strStringValueName end if ' This code deletes a registry value ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"             ' e.g., Software\Rallencorp strStringValueName = "<ValueName>"  ' e.g., Version strComputer = "<ServerName>"        ' e.g., server01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.DeleteValue(HKLM, strKeyPath, strStringValueName) if intRC <> 0 then    WScript.Echo "Error deleting value: " & intRC else    WScript.Echo "Successfully deleted value: " & strStringValueName end if

Discussion

Registry keys are used to structure the registry. Registry values are to files what registry keys are to folders. This simple analogy helps describe the purpose of registry values: to store data. Values are made up of three elements: value name, value datatype, and value data. The datatype defines the type of data the value can contain. There are 11 total datatypes; I list the six most common in Table 9-3. If you want to see the more obscure datatypes, see MS KB 256986.

Table 9-3. Most common registry datatypes

Datatype

Description

REG_DWORD

A double word (DWORD). A single word is a 16-bit number, so a double word is a 32-bit number (range from 0-4,294,967,296). Often, this datatype is used when a Boolean value is called for (0 or 1).

REG_SZ

An ASCII or Unicode string. This is another popular datatype, which is frequently used to store names, descriptions, and other text-based data.

REG_MULTI_SZ

Stores multiple independent ASCII or Unicode strings. Each string is null-terminated.

REG_EXPAND_SZ

An ASCII or Unicode string that contains one or more environment variables. This is essentially the same as REG_SZ, except that the embedded environment variables should be evaluated when the data is retrieved by the calling application.

REG_BINARY

Binary data. Avoid storing large binary blobs in the registry with this type so you don't exceed the maximum size of the registry or impact query performance.

REG_LINK

Similar to creating a shortcut on the filesystem, except that a REG_LINK creates a shortcut or link from one section of the registry to another. See Recipe 9.10 for more on creating a REG_LINK.


See Also

Recipe 9.3 and MS KB 256986 (Description of the Microsoft Windows Registry)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

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