Skip to content

Instantly share code, notes, and snippets.

@petemill petemill/IISSites.cs
Created Jun 15, 2013

Embed
What would you like to do?
This simple and small .Net app will read the list of locally-configured IIS sites (filtered to specific binding hostnames if you want) and output: - The name of the site - The hostnames bound to the site - The phsical local directory running the site - If the directory is a git repository, the remote url of the repository and the branch the loca…
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Web.Administration;
using NGit.Api;
using Sharpen;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
//IIS management object
ServerManager srv = new ServerManager();
//get the IIS sites we're concerned with
var sitesRaw = (args != null && args.Length != 0)
? srv.Sites.Where(site => site.Bindings.Any(binding => binding.Host.Contains(args[0])))
: srv.Sites;
//read the information we care about
var sites = sitesRaw.Select(site => new SiteWithDir()
{
Name = site.Name,
Paths = site.Applications.SelectMany(application => application.VirtualDirectories.ToDictionary(vDir => vDir.PhysicalPath, vDir => GetGitRemote(vDir.PhysicalPath))),
Hosts = site.Bindings.Select(binding => binding.Host)
});
//pretty print it
sites.ToList().ForEach(Console.WriteLine);
}
private static string GetGitRemote (string localGitPath)
{
try
{
var repo = Git.Open(new FilePath(localGitPath)).GetRepository();
return repo.GetConfig().GetString("remote", "origin", "url") + "(" + repo.GetBranch() + ")";
}
catch (Exception ex)
{
return ex.Message;
}
}
public class SiteWithDir
{
public string Name { get; set; }
public IEnumerable<KeyValuePair<string,string>> Paths { get; set; }
public IEnumerable<string> Hosts { get; set; }
public override string ToString()
{
return Name + Environment.NewLine +
"-----------" + Environment.NewLine +
String.Concat(Hosts.Select(host => host + Environment.NewLine)) +
String.Concat(
Paths.Select(path => " - " + path.Key + Environment.NewLine
+ " -- " + path.Value + Environment.NewLine)
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.