Search code examples
c#iisiis-7iis-6directoryservices

Programatically determine if IIS site is running or not


I have a script that gathers up all sites in IIS and emails a few details for auditing. I want to adjust it so that it only emails sites that are running. I do not need to know about sites that are stopped. I already have a reference to all of the DirectoryEntrys in IIS but I don't see any properties that would indicate if it is running or not.

How is this done? Ideally this should run on both IIS6 and IIS7.


Solution

  • The DirectoryEntry.Properties collection, contains a ServerState property. It's not documented very well but I found this blogger that created his own enumeration which appears to be correct. The enum is

    public enum ServerState
    {
        Unknown = 0,
        Starting = 1,
        Started = 2,
        Stopping = 3,
        Stopped = 4,
        Pausing = 5,
        Paused = 6,
        Continuing = 7
    }
    

    Using this, the logic to check if a DirectoryEntry is running, you would use:

    DirectoryEntry entry;
    ServerState state = (ServerState)Enum.Parse(typeof(ServerState), entry.Properties["ServerState"].Value.ToString())
    if (state == ServerState.Stopped || state == ServerState.Paused)
    {
        //site is stopped
    }
                            {