Creating and Deleting Nodes
The most obvious examples of new configuration data that you might need to create are new Web sites or virtual directories. The script that Listing 7, page 60, shows creates a virtual directory in the default Web site when you invoke the script with the virtual directory name and a path to the virtual directory's contents. The script takes two arguments from the command line: the name of the new virtual directory and the physical path. (If these two parameters aren't passed to the script, it terminates.) The script calls the GetObject function to retrieve an object from an existing metabase path, then calls the IADsContainer interface's Create method to create a new node. (For more information about the ADSI IADsContainer interface, see "Related Reading.") This method also takes two arguments: the new node's class and name. The script then sets the physical path and calls SetInfo() to commit the changes. All other properties are inherited from the parent nodes.
Deleting a node is similar to creating it. This task uses the IADsContainer interface's Delete method to perform the delete operation, as Listing 8 shows. In this example, the script takes only one argument: the name of the virtual directory to delete. The script uses the GetObject function to locate the parent node, then uses the IADsContainer Delete method to delete the virtual directory.
Querying for Data
Suppose you want to know where a particular property resides and whether the property is set. For example, suppose you want to determine the level of security compliance on your IIS machine by making sure that your sites and virtual directories allow only a particular type of authentication. To accomplish such tasks, you can use the GetDataPaths function. GetDataPaths returns paths (e.g., IIS://localhost/w3svc/1/root) to all locations in which a specified property exists.
Listing 9 shows a sample script that uses the GetDataPaths method to look up all sites and virtual directories that allow anonymous access. As usual, the script begins by retrieving an object, in this case, the query starting point. (The query will search all subnodes of the specified object.) The script then calls the GetDataPaths method, which takes a string that specifies the property we're looking for. The second argument specifies whether we're looking for any property or only inheritable properties. The GetDataPaths method returns an array of paths, as Figure 3 shows. These results reveal that anonymous authentication is set on the IIS://localhost/W3SVC and IIS://localhost/W3SVC/1/ROOT nodes. To turn off anonymous access, you can use these paths as arguments for additional GetObject calls, as the extended script in Listing 10 shows.
Get Busy
Automating IIS administration can be a big timesaver and a rewarding experience, especially if you administer multiple IIS sites. When you understand the IIS metabase's logical structure and know how to get, set, create, delete, and query IIS configuration data, you can start using your own IIS scripts. Of course, you'll want to add to your scripts some error-handling and parameter-validation functions, which we omitted for the sake of simplicity, but other than that, you're ready to script IIS.
End of Article
Prev. page
1
2
3
4
[5]
next page -->