Previous Page
Next Page

Making the Connection

To get an idea of the types of data accessible from the CIM_Setting element class, you can use the CIMSettingClass.vbs script. This script also illustrates connecting to the MicrosoftIISv2 namespace and using WMI to query for IIS 6.0 configuration information.

Note

To run the scripts listed in this chapter, you will need to have IIS installed on your test server. If IIS is not installed and configured, then the scripts will not work.


CIMSettingClass.vbs

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

strComputer = "."
wmiNS = "/root/MicrosoftIISv2"
wmiQuery = "select * from CIM_Setting"
Set objWMIService = GetObject("winmgmts://" _
  & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

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

Header Information

The Header information section of CIMSettingClass.vbs, which follows, contains the normal Option Explicit, On Error Resume Next, and six variables. The advantage of splitting out the variables instead of including the data on the connection string is that doing so makes the script more portable and easier to modify. Use of the variables is detailed in Table 19-1.

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

Table 19-1. Variables Used in CIMSettingClass.vbs

Variable

Use

strComputer

Holds assignment of target computer name

wmiNS

Holds the WMI namespace

wmiQuery

Holds the WMI query

objWMIService

Holds the connection into the target WMI namespace

colItems

Holds the collection of items that are returned from the WMI query

objItem

Used to iterate through the collection


Reference Information

The Reference information section of the script is used to assign values to the variables that are listed in the Header information section. StrComputer is the target computerthe one that is running IIS 6.0 and the one from which you are trying to obtain information. In this case, you are targeting the server called London. You next use the variable wmiNS to hold the namespace you want to connect into. When working with IIS 6.0, you will use the /root/MicrosoftIISv2 namespace.

You defined the target computer and the target WMI namespace. Next, you define your query. You use the generic "Select *" format and assign the query to the wmiQuery variable. The only tricky issue with querying WMI is how to know which class to target and what properties the class supports. For this information, the best tool is the Platform SDK, which is available at http://www.msdn.microsoft.com. You can download a copy of it and install it on your laptop. (It makes for great reading while you are sitting on the beach in Kauai. The only problem is keeping sand out of the keyboard.) Pursuant to our earlier discussions, you will query the CIM_Setting element class for names of all the read/write properties for the IIS 6.0 admin object.

The last task you need to complete in the Reference section is setting the colItems variable equal to the data returned from running the ExecQuery method when you feed it your WMI query. The Reference section of the script is seen below.

strComputer = "london"
wmiNS = "/root/MicrosoftIISv2"
wmiQuery = "select * from CIM_Setting"
Set objWMIService = GetObject("winmgmts://" _
  & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

Worker and Output Information

The Worker and Output information section of the script is small because most of the real work was done in the Reference information section. Because you have a collection of items that comes back from the WMI query, you need to iterate through the collection to display the information. The easiest way to iterate through the collection is to use the For Each...Next construction. To represent the present record being worked with, objItem is used as a placeholder. Once you issue the next command, you move to the next record in the stream and assign it to the objItem variable. You then simply use WScript.Echo to echo out the name of the item in the collection. The Worker and Output section of the script is seen below.

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


Previous Page
Next Page