Getting SMS Objects

Now that you are comfortable developing and using scripts, you can concentrate on the mechanics of SMS scripting. The best place to start is by retrieving SMS data. Such scripts should not change any SMS configuration details, and therefore you are not at risk of causing adverse effects.

Getting SMS objects is often the first thing that any script you write has to do. Until you get objects, you cannot change or delete them. However, getting SMS objects in a script can also be the easiest way to get information from SMS. When using a script, you do not have to open Microsoft Management Console (MMC) and the SMS Administrator console and then navigate through the console tree. If you keep a command prompt window open (as many administrators do), all you have to do is enter the name of the script and possibly some relevant parameters.

Note

  • WMI scripting can be done with several different WMI scripting models. All examples in this appendix use the WMI session model (unless otherwise noted), which is the most flexible model. For more information about WMI scripting models, see the WMI SDK. Also, not all script examples include the lines to create the locator and connect to the server. You can copy these lines from scripts that do have them.

The most common and flexible method to retrieve SMS data in a script is to execute a query, as shown with the following line:

Set Machines = WbemServices.ExecQuery( "Select * From SMS_R_System" )

The query syntax is the same as what is used in the SMS Administrator console, which uses the same WMI Query Language (WQL) syntax.

If you need to retrieve a particular instance (not a set of instances), and the value for looking up the instance is contained in a key property, it is faster and more efficient to get the instance (instead of querying for it), as shown in the following line:

Set Machine=WbemServices.Get( "SMS_R_System.ResourceID=5" )

For information about the available SMS classes and their keys, see the Class Schema Reference in the Microsoft Systems Management Server 2003 Software Development Kit. The best way to understand the SMS classes is to browse them, as described in Appendix B, "Windows Management Instrumentation."

If you cannot use the WMI Get method (because you do not have the value of a key property to find the instance you need), you have to do a query and enumerate the set of returned results and use the details.

Sample C.3 shows querying for SMS objects from a Visual Basic program. Sample C.4 is the equivalent WSH script.

Sample C.3 Query1.bas - a Visual Basic program that uses a query to get SMS objects

Private WbemServices As SWbemServices
Private Sub Form_Load()
    Dim loc As New SWbemLocator
    Set WbemServices = loc.ConnectServer(, "root\SMS\site_B2K")
       Dim Machine As SWbemObject
       Dim Machines As SWbemObjectSet
       Set Machines = WbemServices.ExecQuery("Select * From SMS_R_System")
       For Each Machine In Machines
           MsgBox "Got system " + Machine.Name +": "+ Machine.IPAddresses(0)
       Next
End Sub

Sample C.4 Query1.vbs - a WSH equivalent to Sample C.3

Dim loc
Set loc = CreateObject("WbemScripting.SWbemLocator")
Dim WbemServices
Set WbemServices = loc.ConnectServer( ,"root\SMS\site_B2K")
Dim Machine
Dim Machines
Set Machines = WbemServices.ExecQuery("Select * From SMS_R_System")
For Each Machine In Machines
    MsgBox "Got system " + Machine.Name
Next

Note

  • Remember to change the B2K site code in the previous two examples to the site code for your site. Also, the examples are intended to run on the site server itself. If you want to run them remotely, you have to add the server and possibly the user name and password details on the ConnectServer line, as described in the "Connecting to WMI" section earlier in this appendix.

When returning results in your script, some properties might require special treatment, such as date or array properties. You might have to include code to enumerate the array elements or to parse the date values. The samples in this appendix include examples of how to work with properties.

If you are returning more than one class in your query, you must be more specific when referring to the properties. For example, when using the results from Sample C.5, you must refer to the returned properties as Machine.CS.Name and Machine.NW.MacAddress (as opposed to Machine.Name and Machine.MacAddress).

Sample C.5 Query2.vbs - part of a script to query two classes at once

Set Machines = gService.ExecQuery("Select CS.Name, NW.MACAddress
 From SMS_G_System_COMPUTER_SYSTEM CS, SMS_G_System_NETWORK_ADAPTER NW 
 WHERE CS.Name='"+ computername +"' AND NW.ResourceID=CS.ResourceID")
For More Information

Did you find this information useful? Please send your suggestions and comments about the documentation to smsdocs@microsoft.com.