Saturday, February 6, 2016

Get websites and virtual subdirectory websites in IIS 6, 7, 8 - C#

This method will get basic site information from IIS for primary sites and virtual directory sites one level under the primary sites.

This was inspired by other blog posts and code snippets but none of them took the additional step of getting the information about virtual sub directories, which can also be websites in an of themselves. (e.x. domain.com/subdomain)


Usage: GetSites("IIS://yourwebserver-name/W3SVC");


      
        private static List<Website> GetSites(string path)
        {

            DirectoryEntry isEntities = new DirectoryEntry(path, "a_domain_user", "a_domain_password");
            List<Website> webList = new List<Website>();
            var sites = from s in isEntities.Children.OfType<DirectoryEntry>()
                where s.SchemaClassName == "IIsWebServer"
                select s;

 
            if (sites.Any())
            {
                foreach (var s in sites)
                {
                    webList.Add(new Website
                    {
                        Identity = Convert.ToInt32(s.Name),
                        Name = s.Properties["ServerComment"].Value.ToString(),
                        PhysicalPath = (from p in s.Children.OfType<DirectoryEntry>()
                            where p.SchemaClassName == "IIsWebVirtualDir"
                            select p.Properties["Path"].Value.ToString()).Single(),
                        Status = (ServerState) s.Properties["ServerState"].Value
                    });
                }

                foreach (var d in sites)
                {
                    Console.WriteLine(d.Properties["ServerComment"].Value.ToString());
                   
                    var virtualSubDirsQry = (from s in d.Children.OfType<DirectoryEntry>()
                                                select s);

                    var virtualSubDirsQry2 = (from s in virtualSubDirsQry
                                                where s.SchemaClassName == "IIsWebVirtualDir"
                                                select s);

                    foreach (var f in virtualSubDirsQry2)
                    {
                        var virtualSubDirsQry3 = (from s in f.Children.OfType<DirectoryEntry>()
                                                    where s.SchemaClassName == "IIsWebVirtualDir"
                                                    select s);

                        foreach (var g in virtualSubDirsQry3)
                        {
                            webList.Add(new Website
                            {
                                Identity = 0,
                                Name = g.Name,
                                PhysicalPath = g.Properties["Path"].Value.ToString(),
                                Status = ServerState.Unknown
                            });
                        }

                    }
                        
                }

            }

            return webList;
        }



    public partial class Website
    {
        public int Identity
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string PhysicalPath
        {
            get;
            set;
        }

        public ServerState Status
        {
            get;
            set;
        }
    }

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


No comments:

Post a Comment