Previous Page
Next Page

Where Is the Where Clause?

To more easily modify the Where clause in a script, substitute the Where clause with a variable. This configuration can be modified to include command-line input as well. This is seen in the ListSpecificWhereVariableShares.vbs script.

ListSpecificWhereVariableShares.vbs

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

strComputer = "."
wmiNS = "\root\cimv2"
vWhere = " name = 'C$'"
wmiQuery = "Select Name, path, allowMaximum from win32_Share where " & vWhere
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Path: " & objItem.path
  WScript.Echo "AllowMaximum: " & objItem.AllowMaximum
  WScript.Echo

Next

Let's return to our scenario in which you are looking for shares that have not been limited by the number of connections. You can modify the vWhere variable to look for AllowMaximum = 'true'. It would look like the following:

strComputer = "."
wmiNS = "\root\cimv2"
vWhere = " AllowMaximum = 'true'"
wmiQuery = "Select Name, path, allowMaximum from Win32_Share where " & vWhere
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem In colItems
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Path: " & objItem.path
  WScript.Echo "AllowMaximum: " & objItem.AllowMaximum
  WScript.Echo

Next

Quick Check

Q.

To limit the specific data returned by a query, what WQL statement can be utilized?

A.

The Where clause is very powerful in limiting the specific data returned by a query.

Q.

What are three possible operators that can be employed in creating powerful Where clauses?

A.

The equal sign and the greater than and less than symbols can be used to evaluate the data prior to returning the data set.


Running against multiple computers

1.
Open the wmiTemplate.vbs template in Notepad or your favorite script editor. Save the file as YourNameMultipleComputerMouse.vbs.

2.
In the Header section of the script, declare two new variables (strComputers, aryComputers) that will be used to hold the string of computer names to target, as well as the array that will be created later. This is seen here:

Dim strComputers         'string of several computers
Dim aryComputers         'an array of computers

3.
Assign a few computer names to the strComputers variable. Use any computer reachable via your network, or you can use the ones listed here:

strComputers = "localhost,127.0.0.1,loopback"

4.
Use the Split function to turn strComputers into an array. Assign the array to the variable aryComputers, as seen below:

aryComputers = Split(strComputers,",")

5.
Modify the WMI query so that it chooses the Handedness property from the WIN32_PointingDevice WMI class. The query will look like the following:

wmiQuery = "Select Handedness from win32_pointingdevice"

6.
Use WScript.Echo to print out the WMI query. This will be a header line for the output.

WScript.Echo wmiQuery

7.
Modify the Output section of the script to echo out the Handedness property value. This will be the only line in the For Each...Next loop that iterates through colItems. Use vbTab to space over the output. This is seen below:

For Each objItem in colItems
     WScript.Echo vbTab & "handedness: " & objItem.handedness
Next

8.
Use For Each...Next to walk through the array. Use the strComputer variable to hold an individual computer from the array. Make sure you close out the loop by putting Next as the last line in the script, as seen below:

For Each strComputer In aryComputers
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
   Set colItems = objWMIService.ExecQuery(wmiQuery)

   For Each objItem in colItems
     WScript.Echo vbTab & "handedness: " & objItem.handedness
   Next
Next

9.
Under the For Each strComputer In aryComputers line, use WScript.Echo to print out the name of the computer being queried. This value is contained in the strComputer variable.

WScript.Echo "Computer: " & strComputer

10.
Save and run the script using CScript. Your output will be similar to the output below. If it is not, then compare your script with \My Documents\Microsoft Press\VBScriptSBS\ch10\MultipleComputerMouse.vbs.

Select Handedness from win32_pointingdevice
Computer: localhost
   handedness: 2
   handedness: 2
Computer: 127.0.0.1
   handedness: 2
   handedness: 2
Computer: loopback
   handedness: 2
   handedness: 2


Previous Page
Next Page