Creating Targeted Queries Using AND or OR

Microsoft® Windows® 2000 Scripting Guide

By using the AND and OR keywords, you can create more complex queries, queries that either narrow or expand the range of a query that uses a WHERE clause.

The AND keyword lets you limit the scope of a query. For example, suppose you want a list of autostart services that are stopped. This query requires two parameters in the WHERE clause: the State property, which must be equal to Stopped, and the StartMode, which must be equal to Auto. To create a query such as this, include both parameters, separating them with the AND keyword. In this query, an instance will be returned only if it meets both criteria.

strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colServices = objSWbemServices.ExecQuery _
  ("SELECT * FROM Win32_Service WHERE State = 'Stopped' AND StartMode = 'Auto'")

For Each objService In colServices
    Wscript.Echo objService.Name
Next

The OR keyword, by contrast, allows you to expand the scope of a query. For example, suppose you need a list of services that are either stopped or paused. In this case there are two criteria, but a service needs to meet only one of these to qualify: Either it needs to be stopped or it needs to be paused.

To create a query such as this, separate the two parameters with the OR keyword. In this query, an instance will be returned as long as it is either stopped or paused.

strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colServices = objSWbemServices.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE State = 'Stopped' OR State = 'Paused'")

For Each objService In colServices
    Wscript.Echo objService.Name
Next