How to Run an SMS Scripting Query

Published : April 11, 2005

Each query within SMS has instance of the SMS Provider class SMS_Query. The queries provided with SMS allow you to query for the systems that have been discovered, all Windows XP Systems, all users, client components experiencing fatal errors, and other information about an SMS site. The SMS Administrator console allows you to see and run the complete list of queries, and you can also get a list of available queries by using a script.

You can also create your own queries. See the SMS 2003 SDK for further details.

A string, such as "SMS040", identifies the query in the SMS_Query property QueryID. When retrieved, a query instance is a collection of matching resources. The following script works by first retrieving the SMS_Query collection for all Windows Server 2003 Systems (SMS040) and then enumerating through the collection to display the name of each resource returned by the query.

On This Page

Example
Compiling the Code
See Also

Example

On Error Resume Next
 
Dim objSWbemServices
Dim ProviderLoc
Dim Location
Dim QueryID
 
Dim objQuery
Dim colQueryResults
 
Set ProviderLoc = GetObject("winmgmts:{impersonationLevel=impersonate}!root/sms:SMS_ProviderLocation")
 
If err.number<>0 Then
wscript.echo "Couldn't get SMS Provider"
    wscript.quit
End If
 
For Each Location In ProviderLoc.Instances_
     If Location.ProviderForLocalSite = True Then
        Set objSWbemServices = GetObject("winmgmts:" & Location.NamespacePath)
    Exit For
   End If
Next
 
QueryID="SMS040" ' change for other queries
 
'Get query.
Set objQuery=objSWbemServices.Get("SMS_Query.QueryID='"+ QueryID +"'" )
 
If err.number<>0 Then
    wscript.echo "Couldn't get Queries"
    wscript.quit
End If
 
'Run query.
wscript.echo objQuery.Name
wscript.echo "----------------------------------"
 
Set colQueryResults=objSWbemServices.ExecQuery(objQuery.Expression)
For Each objResult In colQueryResults
    wscript.echo "     " + objResult.Name
Next
If colQueryResults.Count=0 Then
    wscript.echo "      no query results"
End If
 
Set objSWbemServices=Nothing
Set ProviderLoc=Nothing
Set Location=Nothing
Set objQuery=Nothing
Set colQueryResults=Nothing

Compiling the Code

  • Requires an SMS 2003 Site Server.

See Also

Tasks

How to Enumerate Available Queries on an SMS Site

How to Read SMS Objects