Share via


Lesson 6: Using Associations to Find Metabase Elements

As described in Lesson 3: Organizing Data from the IIS Metabase, there are two association classes in the IIS WMI provider: the CIM_Component class, and the CIM_ElementSetting class. Typically, a user does not create an instance of an association class, but instead creates an instance of a regular class, such as the IIsWebServer class, and asks for instances of the associated classes. For example, server administrators can enumerate all the virtual directories contained in another virtual directory on their server with the following code.

vbscript
Const wbemFlagReturnImmediately = &H10 ' Causes the call to return immediately.
Const wbemFlagForwardOnly = &H20 ' Causes a forward-only enumerator to be returned (faster).
' Make connections to WMI, to the IIS namespace on MyMachine.
set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2")
' Execute the query, putting the results into a SWbemObjectSet object.
strQuery = "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'}"
set vdirsCol = providerObj.ExecQuery(strQuery, , (wbemFlagReturnImmediately OR wbemFlagForwardOnly))
' Report the results. 
For Each vdirObj in vdirsCol
  WScript.Echo "Object: " & vdirObj.Name
Next
jscript
// Make connections to WMI, to the IIS namespace on MyMachine.
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
var providerObj =  
 locatorObj.ConnectServer("janetfi-32", "root/MicrosoftIISv2"); // Execute the query, putting the results into
a  SWbemObjectvar object. strQuery=  "ASSOCIATORS  OF {IIsWebVirtualDir = 'W3SVC/1/Root'}";
var vdirsCol = providerObj.ExecQuery(strQuery);
// Report the results. 
var e = new Enumerator(vdirsCol)
for(; ! e.atEnd(); e.moveNext())
{
  vdirObj = e.item();
  WScript.Echo("Object: " + vdirObj.Name);
}

Data queries that use the ASSOCIATORS OF statement return an SWbemObjectSet object, which is a collection of SWbemObjects. To print out the properties, use the Properties_ property of the SWbemObject as in the code example below.

vbscript
Const wbemFlagReturnImmediately = &H10 ' Causes the call to return immediately.
Const wbemFlagForwardOnly = &H20 ' Causes a forward-only enumerator to be returned (faster).
' Make connections to WMI, to the IIS namespace on MyMachine.
set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2")
' Execute the query, putting the results into a SWbemObjectSet object.
strQuery = "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'}"
set vdirsCol = providerObj.ExecQuery(strQuery, , (wbemFlagReturnImmediately OR wbemFlagForwardOnly))
' Report the results. 
For Each vdirObj in vdirsCol
  WScript.Echo "Object: " & vdirObj.Name
  For Each propSet in vdirObj.Properties_WScript.Echo " " & propSet.Name & " = " & propSet.ValueNext
Next
jscript
// Make connections to WMI, to the IIS namespace on MyMachine.
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
var providerObj = locatorObj.ConnectServer("janetfi-32", "root/MicrosoftIISv2");
// Execute the query, putting the results into a SWbemObjectvar object.
strQuery = "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'} WHERE ResultClass = IIsWebVirtualDir ResultRole = PartComponent";
var vdirsCol = providerObj.ExecQuery(strQuery);
// Report the results. 
var e = new Enumerator(vdirsCol)
for(; ! e.atEnd(); e.moveNext())
{
  vdirObj = e.item();
  WScript.Echo("Object: " + vdirObj.Name);
  var f = new Enumerator(vdirObj.Properties_)for(; ! f.atEnd(); f.moveNext()) {propvar = f.item();WScript.Echo(" " + propvar.Name + " = " + propvar.Value);}
}

The preceding code examples use the following basic query string:

"ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'}"

This query returns all virtual directories under W3SVC/1/Root, including W3SVC/1/Root, the parent W3SVC/1, and all possible children.Normally, a server administrator wants to limit their query by using the properties of association classes. Each element class of the CIM_Component association class has two properties: the PartComponent which is the child member of the association, and the GroupComponent which is the parent. For example, in the containment association of the IIsWebVirtualDir_IIsWebDirectory element class, the IIsWebVirtualDir element of the CIM_ManagedSystemElement class is the GroupComponent, and the IIsWebDirectory element of the CIM_ManagedSystemElement class is the PartComponent. This relationship is viewed best in CIM Studio by clicking on the Associations tab when looking at the IIsWebVirtualDir_IIsWebDirectory element class of the CIM_Component association class.

The results of the queries used in the above code examples can be varied using the following possible query strings.

  • To limit the results of your query to items that are IIsWebVirtualDir objects, and to those that are considered as the child of the association, use the following query string:

    "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'} WHERE ResultClass = IIsWebVirtualDir ResultRole = PartComponent"
    

  • To limit the results of your query to items that are parent virtual directories, use the following query string. On a default installation of IIS 6.0, there are none.

    "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'} WHERE ResultClass = IIsWebVirtualDir ResultRole = GroupComponent"
    

  • To limit the results of your query to items that are IIsDirectory objects, and those that are considered as the child of the association, use the following query string. IIsDirectory is the base class for IIsWebDirectory, IIsWebVirtualDirectory, and IIsWebFile, so any results that match these types are returned.

    "ASSOCIATORS OF {IIsWebVirtualDir='W3SVC/1/Root'} WHERE ResultClass = IIsDirectory ResultRole = PartComponent"
    

For more information on how to construct query strings for the ASSOCIATORS OF statement, see the WMI SDK under WMI Development Environment, WMI Query Language, Data Queries, ASSOCIATORS OF Statement. In that document is the following handy syntax:

ASSOCIATORS OF {ObjectPath} WHERE
    AssocClass = AssocClassName
    ClassDefsOnly
    RequiredAssocQualifier = QualifierName
    RequiredQualifier = QualifierName
    ResultClass = ClassName
    ResultRole = PropertyName
    Role = PropertyName