IIS 6: The Complete Reference
|
|
WMI is an underutilized method of administering Microsoft technologies. It is gaining support, however, and all new Microsoft products are slated to have WMI support built into them. WMI uses the Common Information Model (CIM), along with some Microsoft extensions to CIM. WMI creates a repository of information that can be accessed using a single COM/DCOM (Distributed COM) API.
WMI or ADSI?
WMI has some advantages over ADSI that make it a good fit for administration scripting.
Query Support
WMI uses Structured Query Language (SQL) to select objects and properties. This is great, because you can write a detailed query that will select only the data that you want. When you create the object, you can use either the instancesof, which isn’t supported with every WMI object, or you can use a SQL query; you then step through the query response to get your data.
Association
With WMI, you can select objects and properties that pertain to any number of items, instead of just the ADSI object to which you are linking. This allows you to write more powerful scripts that can affect multiple IIS components.
Universally Supported
As stated earlier, Microsoft is building WMI support into all its products. WMI allows you to access data from and administer all your applications through a single interface. As other vendors start to use WMI, their programs’ data will appear in the WMI repository as well.
User Interface Support
You can get a program from Microsoft called the WMI Object Browser. It is available as part of the WMI SDK version 1.0. When you’re looking for specific objects so you can write scripts for them, the Object Browser is handy, because it lets you graphically drill down to the item you’re looking for. With ADSI, you can look in the metabase to find key names, but that’s about it. With ADSI, it’s not easy to find which methods you can use on an object, but they’re all listed in the WMI Object Browser, shown in Figure 9-2.
Scripting with WMI
Let’s explore some scripting tasks with WMI. First, you must create the object and link to the WMI provider. Here are two lines of code that will allow you to attach to the default web site. The first line attaches to the MicrosoftIISv2 namespace. The second line connects to the default web site object.
SET IISConn = GetObject("winmgmts://mycomputer/root/MicrosoftIISv2") SET MyDefaultWebSite = IISConn.get("IIsWebServerSetting='W3SVC/1'")
After you’ve connected to the web site, you can examine any of the properties for that site and execute any of the supported methods for that site. You can connect to the site to see the properties in two ways: you can directly access the property or you can create a SQL query and then step through the results for that query.
Accessing Properties with WMI
Here is the code that lets you see the default web site server comment, using both connection methods. First, we’ll directly access the property:
Option Explicit DIM IISConn, MyDefaultWebSite SET IISConn = GetObject("winmgmts://mycomputer/root/MicrosoftIISv2") SET MyDefaultWebSite = IISConn.get("IIsWebServerSetting='W3SVC/1'") Wscript.Echo MyDefaultWebSite.ServerComment SET MyDefaultWebSite = Nothing SET IISConn = Nothing
Next, we’ll use the same code, using a SQL query. The result will be the same—it’s just a different way of accessing the code.
Option Explicit DIM IISConnQuery, item SET IISConnQuery = GetObject("winmgmts://mycomputer/root/MicrosoftIISv2")_ .Execquery("Select * from IIsWebServerSetting where Name='W3SVC/1'") FOR each item in IISConnQuery Wscript.echo item.ServerComment NEXT SET IISConnQuery = Nothing
The neat thing about performing SQL queries is that you can select multiple values and multiple objects. Want to see a list with the web site name and the server comment for every web site on this server? Here’s how:
Option Explicit DIM IISConnQuery, item, ServerCommentList SET IISConnQuery = GetObject("winmgmts://mycomputer/root/MicrosoftIISv2")_ .Execquery("Select * from IIsWebServerSetting") FOR each item in IISConnQuery ServerCommentList = ServerCommentList & item.Name &vbtab &_ item.ServerComment &vbcrlf NEXT Wscript.echo ServerCommentList SET IISConnQuery = Nothing
This is where WMI really comes into its own. You can view multiple objects with one simple bit of code. With a little more effort, you can change the server comment for every site. All you have to do is set the ServerComment to some value, finish up with a IISConn.Put_(), and you’re done.The IIS WMI provider can be extremely useful. At this point, not much documentation and code is out there to support it, but as support for WMI grows, it will come.
|
|