Manage IIS 6 in .NET

by Marcel Wijnands 20. August 2008 00:19

If you ever needed to automate the creation of web applications, application pools, virtual directories, etc. you might find this IIsManager ClassLibrary I made to be very useful. Using the System.DirectoryServices namespace to manage IIS 6 is not very straightforward, so I decided to wrap IIS 6 into these classes;

  • IIsService
  • IIsApplicationPool
  • IIsSite
  • IIsDirectory
  • IIsFile
  • IIsVirtualDirectory

After instantiating the IIsService class, you can use it to iterate through sites (using LINQ if you want), adding or removing them, and set properties on them.

The following example will create a website, set it to use ASP.NET 2.0, create an application pool and set the website to use it, changes access permissions on a directory and creates a virtual directory containing a web application.

' Instantiate an IIsService which represents the W3SVC service on the localhost.
Using IIsSvc = New IIsService

    ' Creates an IIsSite.
    Using testsite = IIsSvc.AddSite("TestSite", "c:\inetpub\testsite", "test.site.nl")

        ' Sets AccessPermissions to allow reading and executing scripts (.aspx).
        testsite.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Script

        ' Sets the website to use ASP.NET 2.0
        testsite.ASPNETVersion = ASPNETVersions.v2_0_50727

        ' Create an IIsApplicationPool.
        Dim testpool = IIsSvc.AddAppPool("testpool")

        ' Set the site to use the new application pool.
        testsite.ApplicationPoolId = testpool.Id

        ' Creates an IIsDirectory and changes the AccessPermissions to allow writing to it.
        ' Note that the physical directory already has to exist since we're just creating
        ' metabase information.
        Using images = testsite.AddDirectory("images")
            images.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Write
        End Using

        ' Creates an IIsVirtualDirectory and creates a Web application in it.
        Using newapp = testsite.AddVirtualDirectory("newapp", "c\inetpub\newapp")
            newapp.CreateApplication()
            newapp.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Script
        End Using

        ' Starts the website. It is stopped at creation by default.
        testsite.StartSite()

    End Using

    ' Because the classes implement IDisposable, the Dispose method is automatically called
    ' at 'End Using'.
End Using

You can download the solution below. It's a Visual Studio 2008 solution, but the compiled assembly is also included. It contains some more examples for you to explain the usage of the ClassLibrary.

IIsManager.zip (65.34 kb)

The project is now also hosted on codeplex at http://www.codeplex.com/IIsManager.

Tags:

IIS 6 | VB.NET

Comments

8/21/2008 3:50:50 AM #

Scott H

Nice work.  Wish I had this about 2 weeks ago.  But we might still use it in future web releases.  Kudos to you sir.

Scott H United States

9/13/2008 3:04:10 AM #

sean

Any hints on loading this from powershell ?  I loaded the DLL with Reflection
but cannot seem to create an object with:

$iis = new-object IIsService

any tips ?

sean

12/10/2008 12:48:49 AM #

Marcel Wijnands

I have no experience with PowerShell but this worked for me:

PS Z:\> [System.Reflection.Assembly]::LoadFrom("D:\Coding\IIsManager.dll")

GAC    Version        Location
---    -------        --------
False  v2.0.50727     D:\Coding\IIsManager.dll

PS Z:\> $mySvc = New-Object IIsManager.IIsService


From here on you can use for example it's property Sites like:

PS Z:\> $obj.Sites

Id                    : 1
State                 : Started
AutoStart             : True
Description           : Default Web Site
ApplicationPoolId     : DefaultAppPool
ApplicationName       :
ASPNETVersion         :
DirectoryBrowsing     : 1073741854
DefaultDocuments      : Default.htm,default.aspx,index.htm,index.html,iisstart.htm
AccessPermissions     : 513
AuthenticationMethods : Anonymous
Path                  : C:\inetpub\wwwroot
Directories           : {}
VirtualDirectories    : {}
Files                 : {}

...etc

Marcel Wijnands Netherlands

12/30/2008 5:21:21 AM #

Nate

I'm seeing an "Unknown Error (0x80005000)" using this code on both my development box and on both of our web servers (Server 2003 RC2) right at the point of getting the FirstAppPoolsName in the IIsService class constructor. Any ideas?

Nate Australia

2/6/2009 2:33:35 PM #

Kristof

I got small problem with you code.

I tried running the script on my server. IIS6 is installed etc. I'm running sites on it that are on the internet avaible so, geuss that works correctly. So I'm doing something differend wrong.

Annyway I get this error: No IIS Web Service Metabase entry found on this machine.

Kristof Belgium

2/6/2009 3:10:54 PM #

Marcel Wijnands

Kristof,

Is your server running windows server 2003? R2? What Service Packs?

Marcel Wijnands Netherlands

2/6/2009 3:17:42 PM #

Marcel Wijnands

Nate,

What line exactly does the error occur? FirstAppPoolsName is a function, so which line in that function?

Marcel Wijnands Netherlands

2/6/2009 4:56:58 PM #

Kristof

Server 2003
Service pack 1

Kristof Belgium

2/11/2009 3:32:44 PM #

Kristof

Got it working. Had do do with some rights etc...
Annyway got annother question. Is there someting in your code to put the bandwidth throttling and web site connections???

Kristof Belgium

2/11/2009 8:54:55 PM #

Marcel Wijnands

Hello Kristof,

No, I haven't got that built in. But when you look at the way I set properties, you can build it in yourself if you can find the right properties here: msdn.microsoft.com/en-us/library/ms524487.aspx

Hope you find it useful.

Marcel Wijnands Netherlands

2/12/2009 9:14:08 AM #

Kristof

Alright thx allot. I will dig into it

Kristof Belgium

5/5/2009 11:03:43 PM #

mustafa shabib

hey guys --

anyone know how to disable "Integrated windows authentication" in iis6.0 via the directory services?

thanks.

mustafa

mustafa shabib United States

10/3/2009 1:02:40 AM #

shyNET|Reviews

..

shyNET|Reviews United Kingdom

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant