Monday, July 30, 2012

WMI: GetExpandedStringValue() expands %Variables% to their values.

I was working on a VbScript to iterate through registry keys and reading a registry key of type “Reg_Expand_SZ“ which mostly contains Expandable Environment variables. I used WMI method 'GetExpandedStringValue()' to read it but it always expands %Variables% to their values. For instance, if key value contains %userprofile%, it would be expanded to Local User Profile Folder. And this was not my requirement. My need was to read the Value as it is with all %Variables% without expanding them. I even used WMI method 'GetStringValue()' but same result happened. I found a simple solution for this. I kept using WMI to iterate through registry keys, and when I found my required registry key path, instead of using 'GetExpandedStringValue()' or 'GetStringValue()' I used Windows Scripting Host method RegRead() method, which reads the key value without expanding its %Variables% to their values.

VbScript: ReadRead Method.
  1.         Dim WsShell
  2.         Set WsShell=CreateObject("Wscript.Shell")
  3.         sAcadUnNamedProfileACAD=WsShell.RegRead ("HKCU\" & SelecteAcadReg & "\Profiles\<<Unnamed Profile>>\General\\ACAD")

However if you wanted to write this registry value of type “Reg_Expand_SZ“ then consider using WMI Method ‘SetExpandedStringValue()’ . 
 
 

No comments: