我可以在 CN=Configuration,DC=domain,DC=local 命名上下文中使用 Active Directory LDAP 查询来完成。我在这里分享代码:
public static string getExchangeServerVersion()
{
try
{
string domain =Domain.GetCurrentDomain().ToString();
DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/rootDSE", domain));
DirectoryEntry objDirectoryEntry = new DirectoryEntry(string.Format("LDAP://{0}/{1}",domain,rootDSE.Properties["configurationNamingContext"].Value.ToString()));
DirectorySearcher searcher = new DirectorySearcher(objDirectoryEntry, "(&(objectClass=msExchExchangeServer))");
SearchResultCollection col = searcher.FindAll();
string version = string.Empty;
foreach (SearchResult result in col)
{
DirectoryEntry user = result.GetDirectoryEntry();
if (String.Equals(user.Properties["name"].Value.ToString(),Dns.GetHostName(),StringComparison.InvariantCultureIgnoreCase))
{
version = user.Properties["serialNumber"].Value.ToString();
break;
}
}
return version;
}
catch (Exception ex)
{
Console.WriteLine("\nError : " + ex.Message);
return "";
}
}
主功能 :
static void main()
{
string exchangeServerVersion = string.Empty;
exchangeServerVersion =getExchangeServerVersion();
if (exchangeServerVersion.Contains("Version 6"))
{
users.GetExchange2003UserList();
GetADGroupList();
}
else if (exchangeServerVersion.Contains("Version 8"))
{
users.GetExchange2007UserList();
GetADGroupList();
}
else if (exchangeServerVersion.Contains("Version 14"))
{
users.GetExchange2010UserList();
GetADGroupList();
}
}
由于所有版本都支持 ldap。希望它适用于每个人。
如果还有其他方法可以从 C# 获取交换版本,请分享。