IIS Automation (6/6)

Getting the list of websites in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Web.Administration;

namespace IISWebsiteList
{
    class Program
    {
        static void Main(string[] args)
        {
            ServerManager serverManager = new ServerManager();
            Console.Out.WriteLine("Sites count: " + serverManager.Sites.Count);
            foreach (Site site in serverManager.Sites)
            {
                Console.Out.WriteLine("Website name: " + site.Name);
            }
        }
    }
}

Getting the list of websites in C++

#include <ahadmin.h>
#include <iostream>
#include <comutil.h>    // for _bstr_t

void PrintSiteConfig(IAppHostElement* siteElement)
{
    _bstr_t nameAttribute(L"name");
    IAppHostProperty* nameProperty = NULL;
    HRESULT hr = siteElement->GetPropertyByName(nameAttribute, &nameProperty);
    if (SUCCEEDED(hr))
    {
        BSTR name = NULL;
        hr = nameProperty->get_StringValue(&name);
        if (SUCCEEDED(hr))
        {
            std::wcout << "Website name: " << name << std::endl;
            SysFreeString(name);
        }
        else
        {
            std::cout << "Failed to get name string" << std::endl;
        }

        nameProperty->Release();
    }
    else
    {
        std::cout << "Failed to get name property" << std::endl;
    }
}

void PrintSitesCollectionConfig(IAppHostElementCollection* sitesCollection)
{
    DWORD count = 0;
    HRESULT hr = sitesCollection->get_Count(&count);
    if (SUCCEEDED(hr))
    {
        std::cout << "Sites count: " << count << std::endl;

        for (DWORD i = 0; i < count; ++i)
        {
            VARIANT variantIndex;
            variantIndex.vt = VT_UI4;
            variantIndex.ulVal = i;

            IAppHostElement* siteElement = NULL;
            hr = sitesCollection->get_Item(variantIndex, &siteElement);
            if (SUCCEEDED(hr))
            {
                PrintSiteConfig(siteElement);
                siteElement->Release();
            }
            else
            {
                std::cout << "Failed to get site element" << std::endl;
            }
        }
    }
    else
    {
        std::cout << "Failed to get sites collection count" << std::endl;
    }
}

int main(int argc, char* argv[])
{
    // Initialize COM subsystem
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (SUCCEEDED(hr))
    {
        // Create an admin manager
        IAppHostAdminManager* adminManager = NULL;
        hr = CoCreateInstance(__uuidof(AppHostAdminManager), NULL, CLSCTX_INPROC_SERVER,
            __uuidof(IAppHostAdminManager), (void**)&adminManager);
        if (SUCCEEDED(hr))
        {
            _bstr_t configPath(L"MACHINE/WEBROOT/APPHOST");
            _bstr_t sectionName(L"system.applicationHost/sites");
            IAppHostElement* sitesSection = NULL;
            hr = adminManager->GetAdminSection(sectionName, configPath, &sitesSection);
            if (SUCCEEDED(hr))
            {
                IAppHostElementCollection* sitesCollection = NULL;
                hr = sitesSection->get_Collection(&sitesCollection);
                if (SUCCEEDED(hr))
                {
                    PrintSitesCollectionConfig(sitesCollection);
                    sitesCollection->Release();
                }
                else
                {
                    std::cout << "Failed to get sites collection" << std::endl;
                }
                        
                sitesSection->Release();
            }
            else
            {
                std::cout << "Failed to get system.applicationHost/sites element" << std::endl;
            }

            adminManager->Release();
        }
        else
        {
            std::cout << "Failed to create IAppHostAdminManager" << std::endl;
        }
        
        // Cleanup COM subsystem
        CoUninitialize();
    }
    else
    {
        std::cout << "Failed to initialize COM subsystem" << std::endl;
    }

    return 0;
}

blog comments powered by Disqus

Copyright(c) 2006-2015 Xavier Leclercq | Privacy policy

Home
Contact Us
Search