Search code examples
iisactive-directoryadsi

How to view all properties of an IIS site (ADSI Object)?


I have done a lot of background work on this before posting it here. I would like to discover all the IIS sites that are running in a machine using their Active Directory Service Interface (ADSI) objects. SO far, I've found programmatic methods to discover these.

  • In c++ we can use the com interface method ADsOpenObject to get the object properties.
  • In c# we can do the same, but I'm having some troubles getting the dlls to work.
  • In js we can use this script to achieve the same, but there are some constraints in obtaining all properties (for example, handling safe arrays)

Is there any way to view ADSI objects in GUI with all the properties or like a browser which can list all the IIS site objects found? Or a proper script/snippet/cmdlet which can achieve the same?


Solution

  • I just tried it in C# and it was super easy. No need to use ADSI. But yes, if you're not using .NET Core, it can be tricky getting the DLL reference to work. There are a lot of answers here, but the easiest way is to manually edit your .csproj file, and add this with your other references (inside the <ItemGroup> tag):

    <Reference Include="Microsoft.Web.Administration">
        <HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>
        <SpecificVersion>False</SpecificVersion>
    </Reference>
    

    You do need to make sure you have the IIS Management Console installed, as the first part of this answer describes.

    In .NET Core you can just install the Microsoft.Web.Administration NuGet package.

    Then you can do this to list all the sites in IIS on the local machine:

    using (var serverManager = new ServerManager()) {
        foreach (var site in serverManager.Sites) {
            Console.WriteLine(site.Name);
        }
    }