Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
As noted previously, one of the major limitations of the WMI Query Language (WQL) is the fact that you cannot specify a sort order of any kind; all you can do is request a collection of data and then display the data in the order in which WMI returns it. For example, service instances are always returned in alphabetical order. You cannot modify your WQL query to instead sort the data by service state or by the user account under which the service runs.
If you want to display data in an alternative fashion (and if you do not want to use the tabular data control), you will have to write code that can sort and then display the data. Traditionally, script writers have done this by placing the data in an array and then using sorting algorithms (such as the "bubble sort", an algorithm that iterates through a list of elements, swapping adjacent pairs that are out of order) to sort the data.
Unfortunately, these sorting algorithms can become quite complicated, particularly if the data set contains a number of fields (service name, service status, service account, and so forth). A much easier way to sort information is to place this information in a disconnected recordset and then use a single line of code to sort the information.
A disconnected recordset is essentially a database that exists only in memory; it is not tied to a physical database. You create the recordset, add records to it, and then manipulate the data, just like any other recordset. The only difference is that the moment the script terminates, the recordset, which is stored only in memory, disappears as well.
To use a disconnected recordset to sort data, you must first create the recordset, adding any fields needed to store the data. After you have created the fields, you then use the AddNew method to add new records to the recordset, using the same process used to add new records to a physical database. After the recordset has been populated, a single line of code can then sort the data on the specified field. For example, this line of code sorts the recordset by the field ServiceAccountName:
DataList.Sort =
"ServiceAccountName
"
Listing 17.14 contains a script that sorts data by using a disconnected recordset. To carry out this task, the script must perform the following steps:
This query returns a collection consisting of all the services installed on the computer.
Listing 17.14 Sorting Data by Using a Disconnected Recordset
|
|
Send us your feedback | « Previous | Next » |