[英]List all virtual directories in IIS 5,6 and 7
I am trying to create a list of all virtual directories within an IIS site. 我正在尝试创建IIS站点中的所有虚拟目录的列表。 However, I have found that trying to do this varies dramatically in the older versions of IIS.
但是,我发现在旧版本的IIS中尝试执行此操作会有很大差异。 In IIS 7 this is a relatively easy task via C# but I can't seem to find a good method for doing this in IIS 6 and 5.
在IIS 7中,通过C#这是一个相对容易的任务,但我似乎无法在IIS 6和5中找到一个很好的方法。
I have tried using the System.DirectoryServices.DirectoryEntry but that doesn't seem to give me the desired output. 我已经尝试使用System.DirectoryServices.DirectoryEntry,但似乎没有给我所需的输出。
I am the server administrator so I'm open to using other things such as .vbs files that are built into IIS as well as writing my own code. 我是服务器管理员,因此我愿意使用其他内容,例如内置于IIS中的.vbs文件以及编写自己的代码。
Here are two examples that should work across IIS 5, 6 and 7 (with IIS 6 WMI compatibility installed). 以下两个示例应该适用于IIS 5,6和7(安装了IIS 6 WMI兼容性)。 I successfully tested both with IIS 5 and 7.
我成功测试了IIS 5和7。
VBScript version VBScript版本
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 C#版本
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);
}
}
Here are two helper functions to add onto Garetts answer. 这里有两个辅助函数可以添加到Garetts的答案上。 With these, you can loop through each domain and get all its virtual directories, including ones that are not in the domains root folder.
通过这些,您可以遍历每个域并获取其所有虚拟目录,包括不在域根文件夹中的目录。
Get Site ID from domain name: 从域名获取站点ID:
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 entries (recursively) for site entry 获取站点条目的所有后代条目(递归)
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();
}
Mapping Site ID's for large number of sites 映射大量站点的站点ID
Edit: After testing this with a server containing several hundred domains, GetSiteID() performs very slow because it is enumerating all the sites again and again to get the id. 编辑:在使用包含数百个域的服务器对此进行测试后,GetSiteID()执行速度非常慢,因为它一次又一次地枚举所有站点以获取ID。 The below function enumerates the sites only a single time and maps each one to its id and stores it in a dictionary.
以下函数仅枚举站点一次,并将每个站点映射到其id并将其存储在字典中。 Then when you need the site id you pull it from the dictionary instead, Sites["domain"].
然后,当您需要站点ID时,请将其从字典中删除,即站点[“域”]。 If you are looking up virtual directories for all sites on a server, or a large amount, the below will be much faster than GetSiteID() above.
如果您正在查找服务器上的所有站点或大量的虚拟目录,则下面的内容将比上面的GetSiteID()快得多。
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;
}
have you tried using GetObject("IIS://ServerName/W3SVC") 你尝试过使用GetObject(“IIS:// ServerName / W3SVC”)吗?
You do it in VBS like this 你在VBS这样做
'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/ 我在这里有一篇关于它的文章 - > http://anyrest.wordpress.com/2010/02/10/how-to-list-all-websites-in-iis/
I know you're asking for VB solutions, I don't really know VB, I'm more of a C# person. 我知道你要求VB解决方案,我真的不懂VB,我更像是一个C#人。 Anyway, here is a little bit of C#.NET code taken from an app I wrote some time ago which lists the IIS virtual directories...
无论如何,这里有一些C#.NET代码取自我前一段时间写的应用程序,其中列出了IIS虚拟目录......
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;
}
}
Hopefully you will be able to get the general idea from that. 希望你能从中得到一般的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.