Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Using Associations in WMI to Find Metabase Elements

IIS 6.0

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.

IIS 5.1 and earlier: The IIS WMI provider is not available.

For example, server administrators can enumerate all the virtual directories contained in another virtual directory on their server with the following code.

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
// Make connections to WMI, to the IIS namespace on MyMachine. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("MyMachine", "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.

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.Value 
  Next 
Next
// Make connections to WMI, to the IIS namespace on MyMachine. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("localhost", "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 
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.