|
|
ADSI allows you to connect to various Windows server components as a database connection and modify an object's properties. This is commonly accomplished with VBScript, but you can also change an object's properties using any Component Object Model (COM) or .NET Framework-enabled language. For the purposes of this chapter, we'll be using VBScript.
Note | Even though the ADSI makes you think it may apply only to Active Directory, or perhaps only to domain controllers, the interface is valid for any Windows 2000 and later server. |
The IIS ADSI provider includes an object for each key in the metabase. Thus, you can edit any of the keys in the metabase using ADSI. When you open the metabase file with Notepad, the leftmost keys match up with the objects. Here is the list of the keys, in alphabetical order:
IIsCertMapper
IIsCompressionSchemes
IIsCompressionScheme
IIsComputer
IIsCustomLogModule
IIsFilter
IIsFilters
IIsFtpInfo
IIsFtpServer
IIsFtpService
IIsFtpVirtualDir
IIsIPSecurity
IIsLogModule
IIsLogModules
IIsMimeMap
IIsMimeType
IIsWebDirectory
IIsWebFile
IIsWebInfo
IIsWebServer
IIsWebService
IisWebVirtualDir
To connect to the provider, create an object using the ADsPath. The ADsPath for each service can be found in the 'Location' part of the key in the metabase file. For example, the location for the IIsWebService is /LM/W3SVC. To connect to a specific web site, you use the IIsWebServer key. In the metabase file, the key location for the default web site is /LM/W3SVC/1.
Note | When you're using the location in your code, you do not need to include the /LM; it's used only in the metabase path. |
To bind to an object using ADSI, start by creating the object in your VBScript. ADSI uses Lightweight Directory Access Protocol (LDAP) for queries, so port 389 needs to be open on any firewall in the way. Additionally, you do need to be an administrator to run ADSI on a system. Here's the code to get the default web site object:
SET MyDefaultWebSiteObj = GetObject("IIS://mycomputer/W3SVC/1")
Now you can reference the default web site object with the variable MyDefaultWebSiteObj.
Tip | You can connect to any site you wish; all you need to do is attach to the ordinal number of the site. In previous versions of IIS, these numbers were sequential, but in IIS 6, they are generated at random-although the default web site is always number 1. You can find the numbers associated with each site in several places, but the easiest way is to click Web Sites in the MMC; they're listed there. |
The IIS ADSI properties correspond to the properties in the metabase. You can access the properties for an object by querying that object for particular properties. For example, the IIsWebServer has a ServerComment property. You can access that property by getting the parent object and then using that object to get the desired property.
To read the properties of an object, you can reference the property name for the object in your code. Here's a simple VBScript that gets the server comment field from the metabase for the default web site and displays it on the screen:
option explicit DIM MyDefaultWebSiteObj, ServerCommentProp SET MyDefaultWebSiteObj = GetObject("IIS://mycomputer/W3SVC/1") ServerCommentProp = MyDefaultWebSiteObj.ServerComment Wscript.Echo ServerCommentProp SET MyDefaultWebSiteObj = nothing
All the property names can be found in the metabase file. They are too numerous to mention in this book. As you can see in Figure 9-1, several properties exist for the web site object in the MetaBase.xml file. Dozens of properties exist across all the objects in the metabase.
Figure 9-1: The MetaBase.xml file
The IIS ADSI methods are used to perform actions on the associated service. The following standard ASDI methods are used:
Get Retrieves the value for the object property
GetEx Works like Get, but can also retrieve multivalued properties
GetInfo Reloads the object with the property values from the metabase
GetInfoEx Same as GetInfo, but with multivalued property support
Put Sets the value for an object's property
PutEx Sets value(s) for single or multivalued properties
SetInfo Writes the properties to the metabase
Let's take a look at some code that can be used to set a value in the metabase. We'll use the ServerComment from the previous example. This code changes the comment on the web site to 'My new site'. You can use this code in a VBScript.
option explicit DIM MyDefaultWebSiteObj, SvrComment SET MyDefaultWebSiteObj = GetObject("IIS://localhost/W3SVC/1") MyDefaultWebSiteObj.Put "ServerComment", "My new site" MyDefaultWebSiteObj.SetInfo SET MyDefaultWebSiteObj = nothing
You can also use ADSI methods to perform actions on the sites themselves. For example, here's some code that will start and stop the default web site:
option explicit DIM DefaultWebSiteObj SET DefaultWebSiteObj = GetObject("IIS://localhost/w3svc/1") DefaultWebSiteObj.Stop SET DefaultWebSiteObj = nothing
To start the default web site, just use Start in place of Stop in this code.
Generally, it's better that you run these VBScripts through cscript.exe instead of wscript.exe, because any errors or messages will appear in a pop-up with wscript and will appear in the command window with cscript. You can edit anything in the metabase using the ADSI provider-it's just a matter of discovering the object that corresponds to what you want to modify.
|
|