List all virtual directories in IIS 5.6 and 7

I am trying to create a list of all the virtual directories on the IIS site. However, I found that trying to do this varies greatly in older versions of IIS. In IIS 7, this is a fairly simple task using C #, but I cannot find a good method for this in IIS 6 and 5.

I tried using System.DirectoryServices.DirectoryEntry, but this does not seem to give me the desired result.

I am the server administrator, so I am open to using other things, such as .vbs files that are built into IIS, as well as writing my own code.

+4
source share
4 answers

You tried to use GetObject ("IIS: // ServerName / W3SVC")

You do it in vbs like this

'Get the IIS Server Object Set oW3SVC = GetObject("IIS://ServerName/W3SVC/1/ROOT") For Each oVirtualDirectory In oW3SVC Set oFile = CreateObject("Scripting.FileSystemObject") Set oTextFile = oFile.OpenTextFile("C:\Results.txt", 8, True) oTextFile.WriteLine(oVirtualDirectory.class + " -" + oVirtualDirectory.Name) oTextFile.Close Next Set oTextFile = Nothing Set oFile = Nothing Wscript.Echo "Done" 

I have an article about it here → http://anyrest.wordpress.com/2010/02/10/how-to-list-all-websites-in-iis/

0
source

Here are two examples that should work in IIS 5, 6, and 7 (with IIS 6 WMI compatibility installed). I successfully passed testing with both IIS 5 and 7.

VBScript Version

 Function ListVirtualDirectories(serverName, siteId) Dim webSite Dim webDirectory On Error Resume Next Set webSite = GetObject( "IIS://" & serverName & "/W3SVC/" & siteId & "/ROOT" ) If ( Err <> 0 ) Then Err = 0 Exit Function Else For Each webDirectory in webSite If webDirectory.Class = "IIsWebVirtualDir" Then WScript.Echo "Found virtual directory " & webDirectory.Name End If Next End If End Function 

C # Version

 void ListVirtualDirectories(string serverName, int siteId) { DirectoryEntry webService = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteId + "/ROOT"); foreach (DirectoryEntry webDir in webService.Children) { if (webDir.SchemaClassName.Equals("IIsWebVirtualDir")) Console.WriteLine("Found virtual directory {0}", webDir.Name); } } 
+4
source

Here are two helper functions you need to add to your Garetts answer. With their help, you can scroll through each domain and get all its virtual directories, including those that are not in the root folder of the domain.

Get site id from domain name:

  string GetSiteID(string domain) { string siteId = string.Empty; DirectoryEntry iis = new DirectoryEntry("IIS://localhost/W3SVC"); foreach (DirectoryEntry entry in iis.Children) if (entry.SchemaClassName.ToLower() == "iiswebserver") if (entry.Properties["ServerComment"].Value.ToString().ToLower() == domain.ToLower()) siteId = entry.Name; if (string.IsNullOrEmpty(siteId)) throw new Exception("Could not find site '" + domain + "'"); return siteId; } 

Get all descendant records (recursively) for a site record

  static DirectoryEntry[] GetAllChildren(DirectoryEntry entry) { List<DirectoryEntry> children = new List<DirectoryEntry>(); foreach (DirectoryEntry child in entry.Children) { children.Add(child); children.AddRange(GetAllChildren(child)); } return children.ToArray(); } 

Site ID mapping for a large number of sites

Edit: after testing with a server containing several hundred domains, GetSiteID () is very slow because it lists all sites again and again to get the identifier. The function below lists sites only once and compares each of them with an identifier and stores it in a dictionary. Then, when you need the site ID, you pull it out of the dictionary, Sites ["domain"]. If you are looking for virtual directories for all sites on the server or a large amount, then the lower will be much faster than GetSiteID () above.

  public static Dictionary<string, string> MapSiteIDs() { DirectoryEntry IIS = new DirectoryEntry("IIS://localhost/W3SVC"); Dictionary<string, string> dictionary = new Dictionary<string, string>(); // key=domain, value=siteId foreach (DirectoryEntry entry in IIS.Children) { if (entry.SchemaClassName.ToLower() == "iiswebserver") { string domainName = entry.Properties["ServerComment"].Value.ToString().ToLower(); string siteID = entry.Name; dictionary.Add(domainName, siteID); } } return dictionary; } 
+2
source

I know that you are asking for a VB solution, I really don't know VB, I am more a C # person. Anyway, here is some C # .NET code taken from an application that I wrote some time ago that lists IIS virtual directories ...

  using System.DirectoryServices; private DirectoryEntry _iisServer = null; private DirectoryEntry iisServer { get { if (_iisServer == null) { string path = string.Format("IIS://{0}/W3SVC/1", serverName); _iisServer = new DirectoryEntry(path); } return _iisServer; } } private IDictionary<string, DirectoryEntry> _virtualDirectories = null; private IDictionary<string, DirectoryEntry> virtualDirectories { get { if (_virtualDirectories == null) { _virtualDirectories = new Dictionary<string, DirectoryEntry>(); DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName); foreach (DirectoryEntry virtualDirectory in folderRoot.Children) { _virtualDirectories.Add(virtualDirectory.Name, virtualDirectory); } } return _virtualDirectories; } } 

I hope you can get a general idea from this.

0
source

All Articles