Modifying an Item in a Dictionary

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Items added to a Dictionary are not set in stone; in fact, you can modify the value of an item at any time. This allows you to do record keeping within a script. For example, your Dictionary might list the names of servers being targeted by the script. Each time the script is run against a server, the value of that Dictionary item can be changed to indicate whether the script succeeded. Right before the script terminates, it can then print a status report showing you a list of successes and failures.

The script shown in Listing 4.49 creates a Dictionary with three keys: servers atl-dc-01, atl-dc-02, and atl-dc-03. In each case, the item value for the key has been set to "No status." This is done to indicate that status information on the server has not been obtained. The item values are then echoed to the screen.

After the Dictionary has been created, the item value for each of the three keys is changed by using the Item method, with the key name passed as the parameter. For example, the following code statement sets the item value for the key atl-dc-01 to "Available":

objDictionary.Item("atl-dc-01") = "Available" 

After the three item values have been changed, the new item values are echoed to the screen.

Listing 4.49   Modifying the Value of a Dictionary Item

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "atl-dc-01", "No status" objDictionary.Add "atl-dc-02", "No status" objDictionary.Add "atl-dc-03", "No status" colKeys = objDictionary.Keys For Each strKey in colKeys     Wscript.Echo strKey, objDictionary.Item(strKey) Next objDictionary.Item("atl-dc-01") = "Available" objDictionary.Item("atl-dc-02") = "Available" objDictionary.Item("atl-dc-03") = "Unavailable" colKeys = objDictionary.Keys For Each strKey in colKeys     Wscript.Echo strKey, objDictionary.Item(strKey) Next

When the script is run under CScript, the following information appears in the command window:

atl-dc-01 No status atl-dc-02 No status atl-dc-03 No status atl-dc-01 Available atl-dc-02 Available atl-dc-03 Unavailable 

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