Previous Page
Next Page

Selective Data from All Instances

The next level of sophistication (from using Select *) is to return only the properties you are interested in. This is a more efficient strategy. For instance, in the previous example, you did a Select * query and returned a lot of data you weren't necessarily interested in. Suppose you wanted to know only which shares are on each machine. With a simple change to the wmiQuery variable and by deleting a few WScript.Echo commands, you can modify your script to get exactly what you want.

Just the Steps

To select specific data

1.
Make a connection to WMI.

2.
Use the Select statement to choose the specific property you are interested in (for example, Select name).

3.
Use the From statement to indicate the class from which you want to retrieve data (for example, From Win32_Share).


Only two small changes in the ListShares.vbs script are required to enable you to garner specific data via the WMI script. In place of the asterisk in the Select statement assigned in the Reference information section of the script, you substitute the property you want. In this case, only the name of the shares is required.

The second change is to eliminate all unused properties from the Output section. This is very important because the script could fail if you try to echo out a property that is not selected in the Select statement. I said it could fail as opposed to would fail, because if you include On Error Resume Next, the script will work. If you don't include this error handling line of code, the script fails with an "Object does not support this property or method" error. Because this error message is rather confusing, you should be able to recognize it! It is important that you select each item for which you want to return information. In this way, WMI Query Language (WQL) acts just like Structured Query Language (SQL). If you don't select a property, you can't do anything with the property, because to the program, the object doesn't exist. Here is the modified ListName_Only_AllShares.vbs script:

ListName_Only_AllShares.vbs

Option Explicit
On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select Name from win32_Share"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem In colItems
  WScript.Echo "Name: " & objItem.Name
Next


Previous Page
Next Page