I need to write a Web Installer with a bit more complex logic than the standard setup provides, and came across pages like Creating Sites and Virtual Directories Using System.DirectoryServices that deal with .Net classes I have never seen before. (Note: while the page deals with IIS 6, the classes also work on IIS 7 if the IIS 6 Management Compatibility feature is installed)![]()
Trying to figure out what System.DirectoryServices can accomplish, I chose PowerShell to poke around in IIS.
PowerShell needs to be run as administrator, otherwise you will get an “access denied” error (unless you provide login information in the DirectoryEntry constructor)
First, we load the DirectoryServices.dll:
> [system.reflection.assembly]::loadwithpartialname(
"System.DirectoryServices.dll")
Next, we can retrieve the IIS service entry using a DirectoryEntry
> $service = new-object system.directoryservices.directoryentry(
"IIS://127.0.0.1/W3SVC")
> $service.SchemaClassName
IIsWebService
See this chart for the IIS Admin Object Hierarchy.
The children of the IIsWebService are
> $service.Children | select-object SchemaClassName, Path, AppRoot
SchemaClassName Path AppRoot
--------------- ---- -------
IIsFilters IIS://127.0.0.1/W3SVC/FILTERS
IIsApplicationPools IIS://127.0.0.1/W3SVC/APPPOOLS
IIsWebInfo IIS://127.0.0.1/W3SVC/INFO
IIsWebServer IIS://127.0.0.1/W3SVC/1 {}
Let’s get the web server root
> $root = new-object system.directoryservices.directoryentry(
"IIS://127.0.0.1/W3SVC/1/Root")
> $root.Children | select-object SchemaClassName, Path, AppRoot
SchemaClassName Path AppRoot
--------------- ---- -------
IIsWebVirtualDir {D:\MyVirtDir} {}
IIsWebDirectory IIS://127.0.0.1/W3SVC/1/Root/MyWebApp {/LM/W3SVC/1/ROOT/MyWebApp}
An IIsWebDirectory has a defined AppRoot property, whereas an IIsWebVirtualDir has a file system directory as Path, but no AppRoot.
You can instantiate a DirectoryEntry for both IIsWebDirectory and IIsWebVirtualDir using the value of the ADsPath property:
> $root.Children | select-object SchemaClassName, ADsPath, Path
SchemaClassName ADsPath Path
--------------- ------- ----
IIsWebVirtualDir IIS://127.0.0.1/W3SVC/1/Root/MyVirtDir {D:\MyVirtDir}
IIsWebDirectory IIS://127.0.0.1/W3SVC/1/Root/MyWebApp IIS://127.0.0.1/W3SVC/1/Root/MyWebApp

[...] to create Virtual Directories using System.DirectoryServices.DirectoryEntry as described in my previous post. The installer class is called by the web installer using the Custom Actions [...]
[...] to install on a Windows 2008 Server running IIS 7. The prerequisite knowledge is described in my previous [...]