Reading an Expanded String-valued Entry

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Registry values of the expanded string type are strings that include items that are evaluated only when the string value is actually used. For instance, a typical expanded string value is the one that holds the location of the TEMP directory:

%USERPROFILE%\Local Settings\Temp 

The \Local Settings\Temp component of the string is treated literally, but the value of the %USERPROFILE% component can be determined only when it is actually accessed. This is because the value is dependent upon the user accessing the entry. If you look at the registry entry in Regedit.exe, you will see the value %USERPROFILE%\Local Settings\Temp. However, when you retrieve the value of the entry, the value will be something like this: C:\Documents and Settings\jsmith.REDMOND\Local Settings\Temp. In other words, the environment variable %USERPROFILE% is replaced with the folder (in this case, jsmith.REDMOND) that contains the user profile.

The components of expanded string types that appear between percent symbols (%) correspond to environment variables and are dependent either on the aspects of the particular user or the current configuration of the computer.

You use the GetExpandedStringValue method to retrieve expanded string values. The method takes, as one of its parameters, a variable in which the retrieved value is stored. In the retrieved value, the components of the string that appear between percent symbols are automatically evaluated. This means your script will echo a value similar to C:\Documents and Settings\jsmith.REDMOND\Local Settings\Temp rather than a value like %USERPROFILE%\Local Settings\Temp.

Scripting Steps

Listing 16.5 contains a script that retrieves an expanded string value from the registry. 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 registry 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 path of the registry subkey in which the value is located.
  5. Create a variable and set it to the name of the entry.
  6. Use the GetExpandedStringValue method to retrieve the value.
  7. Echo the value to the screen.

Listing 16.5   Reading an Expanded String-valued Entry

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 = "SOFTWARE\Microsoft\Windows Scripting Host\Locations" strEntryName = "CScript" objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _     strEntryName,strValue Wscript.Echo  "The location of the cscript host is: " & strValue

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