How can I use a variable in custom inventory registry path?

Article:HOWTO9242  |  Created: 2005-12-02  |  Updated: 2009-11-04  |  Article URL
Article Type
How To

How can I use a variable in custom inventory registry path?

The Windows registry has many entries with identical structure except for a slight difference in the paths. For example, the Products\InstallProperties entries have identical keys, such as DisplayName, RegCompany, RegOwner, etc. But, each has a unique GUID in the path, as seen here:


Note: The only difference in the above paths is the GUID between "Products" and "InstallProperties".

To simplify the retrieval of data using custom inventory in a situation such as this, it is possible to include a variable in the path rather than hard-code each path individually, as shown in this segment of a custom inventory script:

<%set path="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\"%>
<%set pathb="InstallProperties"%>

<%foreach regkey="prodguid" path="%path%"%>
             c0="<%writexml "key:prodguid.%keyname%"%>"     
             c1="<%writexml "reg:%path%key:prodguid.%keyname%\%pathb%\DisplayName"%>"
             c2="<%writexml "reg:%path%key:prodguid.%keyname%\%pathb%\RegCompany"%>"
             c3="<%writexml "reg:%path%key:prodguid.%keyname%\%pathb%\RegOwner"%>"      

The initial portion of the path is placed into the 'path' variable. The final portion of the path is placed into the "pathb" variable.

The "foreach" loop then traverses each entry in the initial path, setting the variable "prodguid" to the name of the current subdirectory. In other words, we loop through each product, setting prodguid to the value of the product guid in the registry path.

The "c1" line contains:

  1. The "reg" keyword, which tells custinv to return a key's value.
  2. The '%path% variable which holds the initial path.
  3. The key:prodguid.%keyname% variable for the name of the current subdirectory in the registry path.
  4. The remainder of the path.
  5. The key whose value is to be retrieved.

Note: The slashes are necessary to form a valid registry path.

A complete example can be found at KB article 4237

The name of the file is: var_in_reg_path.xml.

Another option is to use the recurse parameter in the foreach loop. See article 23929, "How do I recurse multiple registry keys with custom Inventory for Windows?"

Legacy ID


Article URL

Terms of use for this information are found in Legal Notices