Rem: Interpolating an Array Element to Reference a Property Name

Learn what an interpolated variable is by comparing a scripting language that supports interpolated variables with one that doesn’t.

Bob Wells

August 15, 2004

3 Min Read
ITPro Today logo in a gray background | ITPro Today


Q: I'm having problems with theVBScript code in Listing 3. In this script,Array1 is a dynamic array that's populatedby using Windows ManagementInstrumentation (WMI) to retrieve theproperties of the Win32_Battery class.When I try to use the contents of thisdynamic array to execute a query, I getthe error message Error: Object doesn'tsupport this property or method: 'Array1.'I've tried everything I can think of tomake this script work but nothing hasworked. What am I missing?

A: There are several problems in yourscript. Before I point out the problemsand suggest a possible solution, Ishould take a minute to clarify whatthe script is trying to do for the benefitof our readers. In brief, the script istrying to use Array1 as an interpolatedvariable, as the code at callout C in Listing 3 shows. What does that mean?In the context of scripting languages, tointerpolate means to insert the value ofa variable into an expression, statement,or string. Let's look at a simpleexample in Perl, a scripting languagethat supports interpolated variables.Suppose you have the followingcode in a script:

my $cn = "cn=tmtowtdi";my $user = Win32::OLE->GetObject("LDAP:\\$cn,...")or die;

The snippet begins by assigning thestring "cn=tmtowtdi" to the variablenamed $cn. Next, the code uses thevalue stored in the $cn variable as partof the connection string used to bindto a user object in Active Directory(AD). Notice how the $cn variable ispart of the connection string. That is,the variable is embedded inside thedouble quoted string; there's no stringconcatenation or the like being used.When Perl encounters a variable nameinside a double quoted string, Perlautomatically inserts the variable'svalue at that point in the string—thisis interpolation at work.

VBScript doesn't support interpolatedvariables. For example, thefollowing VBScript version of the previousPerl snippet is invalid:

cn = "cn=tmtowtdi"Set user = _GetObject("LDAP:\cn,...")

To get this code to work in VBScript,you must break the connection stringinto multiple parts and use string concatenationto insert the value of cn, asthis code shows:

cn = "cn=tmtowtdi"Set user = _GetObject("LDAP:\" & cn & ",...")

Now that you know what interpolatedstrings and variables are, let'stake a closer look at the script in Listing3. As I stated earlier, the script containsseveral errors:

  • In the For Each…Next statementthat callout A highlights, you usethe variable i.However, you didn'tinitialize that variable before youused it.

  • The nested For…Next and ForEach…Next statements after thecode at callout A appear to bebackward. My guess is that you'retrying to loop through each item inthe collection returned by Exec-Query, and for each item in the collection,echo the item's properties.As written, the script loops throughthe properties in the array namedArray1 first, and for each propertyin the array, loops through the collectionreturned by ExecQuery.

  • In the code that callout B shows,you set the wmiQuery variable tothe Win32_Battery class query,obtain an instance of the WMIscripting library's SWbemServicesobject, then call that object's Exec-Query method to perform the queryon the local computer—all inside aFor...Next statement. By placing thiscode inside this For loop, you'rerepeatedly performing theseactions, which isn't good scriptingpractice because it wastes valuablesystem resources. This code shouldgo near the beginning of the script,outside any loops, so that theactions are performed only once.

  • In the For Each loop at callout C,you use an interpolated variable,objItem.array1(b), which is thesource of the problem thatprompted your question. However,as I mentioned earlier, VBScriptdoesn't support interpolated variablesor strings.

  • The Win32_Battery class has propertiesof type CIM_ARRAY. However,as written, the script doesn'tsupport properties of typeCIM_ARRAY. Nor does it supportproperties of type CIM_OBJECT.

Based on your script, I'm guessingyou're trying to create a lightweightversion of Scriptomatic. As luck wouldhave it, one already exists called ScriptomaticLite. This tool automaticallyretrieves and displays the values ofeach property in a class. Moreover, itsupports properties of type CIM_www.winnetmag.com/windowsscriptingARRAY and gracefully handles embeddedCIM_OBJECT types. On the WindowsScripting Solutions Web site,you'll find VBScript and Perl versionsof the Scriptomatic Lite code Iadapted for the Win32_Battery class.To download these files, go to http://www.winnetmag.com/windowsscripting, enter 43315 in the Instant-Doc ID box, then click the 43315.ziphotlink. For more information aboutScriptomatic Lite, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting01142003.asp.

The adapted Scriptomatic Litecode, though, doesn't use interpolatedvariables. If you're set on using them,you'll need to use Perl code similar tothe code that Listing 4 shows. Notethat this code doesn't handle propertiesof type CIM_ARRAY or CIM_OBJECT, so you'd need to adapt itaccordingly.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like