# WMI Provider

Windows Management Instrumentation (WMI) provides a way for administrators to leverage Windows® 2000 features. To leverage WMI capabilities, IIS has developed the IIS WMI provider, which provides a set of programming interfaces that offer more powerful and flexible ways to administer your Web sites. The WMI provider offers a better alternative to ADSI to manage configuration in the metabase.

# About WMI

Windows 2000 management tools and scripts use WMI to manage systems composed of numerous devices, resources, and applications. With WMI technology, these systems and components are represented using the Common Information Model (CIM). CIM can model anything in the managed environment regardless of data-source location by providing a consistent and unified view of logical and physical objects. Managed objects are represented using object-oriented constructs, such as classes. These classes include properties that describe data and methods, and the data and methods describe behavior. In addition to data modeling, WMI offers a powerful set of base services that include query-based information retrieval and event notification. Access to these services and to the management data is made possible with a single (COM) programming interface. Complete WMI documentation is available from MSDN and titled "WMI Reference."

# About the IIS WMI Provider

The IIS WMI provider is designed to provide equal functionality as the IIS ADSI provider for metabase configuration and IIS management tasks. The IIS WMI provider accesses a managed object, specifically the IIS metabase, by providing instances of IIS configuration classes to WMI. The IIS WMI classes access the metabase through COM object interfaces; class instances allow your application to access the metabase schema. The IIS WMI provider establishes relationships between objects, allowing you to tie together the various objects and all the objects contained within it, like a virtual directory and all the directories and virtual directories contained within.

The WMI provider reads the metabase as a database of records, where each record is represented as an instance of a class. Each instance can then be queried to determine content, state, associations, and properties. Management tasks can then be performed using an instance of the class.

# ADSI/WMI Provider Comparison

The IIS WMI schema is congruent with the IIS ADSI Admin Objects schema and offers equivalent functionality on the metabase. These two schemas differ only in object and data models. Therefore, a script can be written for a similar task using the ADSI model or the WMI model.

The following table compares the architecture and features of the IIS ADSI provider with those of the IIS WMI provider.

Issue | ADSI provider | WMI provider
Query capabilities | No | Yes, by querying the returned class instances
Object model | COM | COM
Extensible schema | Yes | Yes
Access routes | Scripts and programs | Scripts and programs
Association of related data | Properties are related to metabase keys by containment | An association in WMI describes a relationship between classes and is in itself defined by a class. This powerful concept allows management information about an entire system of related components to be viewed and traversed for tasks such as troubleshooting. Navigating associations to other classes are not necessitated by containment.
Container object | Yes You can use the ADSI container object methods of the IIS Admin Objects to manipulate keys in the metabase. You can create, delete, and move keys by creating, deleting, and moving IIS Admin Objects within container objects. You can also enumerate contained objects such as virtual directories or servers with container object methods. Supports property inheritance. | Yes Associations are used to approximately model metabase containment.

# WMI provider Scenarios and Script Samples

The following samples illustrate some benefits of the WMI provider. These samples are offered only as examples and are not intended for production use.

#### Creating a Web Site

An administrator must create a Web site that includes a set of virtual directories. The following script illustrates how to create a Web site and all the related directories using the IIS WMI provider.

    var strWMIProvider = "winmgmts:root/MicrosoftIISv2";
    var siteName = "W3SVC/3";
    var providerObj;

// Get WMI provider instance

    providerObj = GetObject(strWMIProvider);
    var classObj, serverObj;
    classObj = providerObj.get("IIs_WebServerSetting");
    serverObj = classObj.SpawnInstance_();
    serverObj.Name = siteName;
    serverObj.ServerComment = "WMIsite2";
    serverObj.ServerBindings = new Array(":8080:");
    serverObj.AuthAnonymous = true;
    serverObj.Put_();

// Creating the virtual dir

    var vdirClass, vdirObj;
    vdirClass = providerObj.get("IIs_WebVirtualDirSetting");
    vdirObj = vdirClass.SpawnInstance_();
    vdirObj.Name = siteName + "/ROOT";
    vdirObj.Path = "C:\\inetpub\\wmisite";
    vdirObj.AccessRead = true;
    vdirObj.AccessScript = true;
    vdirObj.AccessWrite = false;
    vdirObj.Put_();

//create the actual fs directory

    var fsObj = new ActiveXObject("Scripting.FileSystemObject");
    fsObj.CreateFolder("c:\inetpub\wmisite");

// Starts the new server

    serverObj = providerObj.get("IIs_WebServer='" + siteName + "'");
    serverObj.Start();

#### Removing Associated Data

An ISP administrator wants to remove a Web site but does not know the site’s ID and only knows the Server’s Comment. Using ADSI, the administrator has to write code to enumerate everything beneath w3svc, and then look for the site that contains the desired ServerComment. With the WMI provider, administrators can easily remove a Web site and all related material using query and delete.

    var providerObj = GetObject("winmgmts:root/MicrosoftIISv2");
    var siteObj = providerObj.ExecQuery("select * from IIS_WebServerSetting where ServerComment = 'WMIsite2'");
    var e = new Enumerator(siteObj);
    for (; !e.atEnd(); e.moveNext()) { e.item().Delete_();
    }

#### Query for Data

An administrator must determine all the virtual directories that have write permissions enabled. Using the underlying query support of WMI, an administrator can execute a quick query on the instances provided by the IIS WMI provider. The following code illustrates a search for all Iis_WebVirtualDirSetting objects, and determines if each object's AccessWrite property is set. An administrator can then cycle through the array of instances returned and perform whatever action is needed, such as setting the AccessWrite bit to FALSE. The following JScript illustrates how to query for this data and to set the AccessWrite bit accordingly.

    var providerObj = GetObject("winmgmts:root/MicrosoftIISv2");
    var arrVdirs = providerObj.ExecQuery("select * from IIs_WebVirtualDirSetting where AccessWrite = true");
    if (arrVdirs.Count > 0) {
       WScript.Echo("These virtual directories have write permission set to true:");
       for (e = new Enumerator(arrVdirs); ! e.atEnd(); e.moveNext()) {
    WScript.Echo(e.item().Name);
    e.item().AccessWrite = false;
    e.item().Put_();
    }
    }
    else {
    WScript.Echo("No vdirs have write permission set");

© 1997-1999 Microsoft Corporation. All rights reserved.