有没有办法使用 C# 从活动目录获取 FSMO 角色?即使可能使用 LDAP 查询也可以。
许多博客上提供了许多 VB 脚本代码。但不是 C#。
以及如何找到一个 DC 是不是 PDC?
谢谢
有没有办法使用 C# 从活动目录获取 FSMO 角色?即使可能使用 LDAP 查询也可以。
许多博客上提供了许多 VB 脚本代码。但不是 C#。
以及如何找到一个 DC 是不是 PDC?
谢谢
我认为找到这个将是一项艰巨的工作,但最终很容易。我正在发布代码以防将来有人需要这个。
System.DirectoryServices.ActiveDirectory.Domain dom = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
System.DirectoryServices.ActiveDirectory.DomainController pdcdc = dom.PdcRoleOwner;
foreach (System.DirectoryServices.ActiveDirectory.DomainController dc in dom.DomainControllers)
{
foreach (ActiveDirectoryRole role in dc.Roles)
{
Console.WriteLine("dc : {0} role : {1}", dc.Name,role.ToString());
}
}
您询问了一种 LDAP 方法来从 Active-Directory 中找到灵活的单主操作(FSMO) 角色。它们可以fsmoRoleOwner
在 Active Directory 的不同命名上下文中查找属性。您可以在这里找到用 C# 编写的 3 个 LDAP 搜索。小心代码不是那么干净,它只是一种概念证明。
一个很好的来源是Determining FSMO Role Holders。
static void Main(string[] args)
{
/* Retreiving RootDSE informations
*/
string ldapBase = "LDAP://WM2008R2ENT:389/";
string sFromWhere = ldapBase + "rootDSE";
DirectoryEntry root = new DirectoryEntry(sFromWhere, "dom\\jpb", "pwd");
string defaultNamingContext = root.Properties["defaultNamingContext"][0].ToString();
string schemaNamingContext = root.Properties["schemaNamingContext"][0].ToString();
string configurationNamingContext = root.Properties["configurationNamingContext"][0].ToString();
/* Search the 3 domain FSMO roles
*/
sFromWhere = ldapBase + defaultNamingContext;
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "pwd");
DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(fsmoRoleOwner=*)";
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("fsmoRoleOwner");
dsLookForDomain.PropertiesToLoad.Add("distinguishedName");
SearchResultCollection srcDomains = dsLookForDomain.FindAll();
foreach (SearchResult sr in srcDomains)
{
/* For each root look for the groups containing my user
*/
string fsmoRoleOwner = sr.Properties["fsmoRoleOwner"][0].ToString();
string distinguishedName = sr.Properties["distinguishedName"][0].ToString();
Regex srvNameRegEx = new Regex("^CN=NTDS Settings,CN=(.*),CN=Servers,.*$");
Match found = srvNameRegEx.Match(fsmoRoleOwner);
if (distinguishedName == defaultNamingContext)
Console.WriteLine("PDC is {0}", found.Groups[1].Value);
else if (distinguishedName.Contains("RID Manager"))
Console.WriteLine("RID Manager is {0}", found.Groups[1].Value);
else
Console.WriteLine("Infrastructure Manager is {0}", found.Groups[1].Value);
}
/* Search the schema FSMO role
*/
sFromWhere = ldapBase + schemaNamingContext;
deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "pwd");
dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(fsmoRoleOwner=*)";
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("fsmoRoleOwner");
dsLookForDomain.PropertiesToLoad.Add("distinguishedName");
srcDomains = dsLookForDomain.FindAll();
foreach (SearchResult sr in srcDomains)
{
/* For each root look for the groups containing my user
*/
string fsmoRoleOwner = sr.Properties["fsmoRoleOwner"][0].ToString();
string distinguishedName = sr.Properties["distinguishedName"][0].ToString();
Regex srvNameRegEx = new Regex("^CN=NTDS Settings,CN=(.*),CN=Servers,.*$");
Match found = srvNameRegEx.Match(fsmoRoleOwner);
if (distinguishedName.Contains("Schema"))
Console.WriteLine("Schema Manager is {0}", found.Groups[1].Value);
}
/* Search the domain FSMO role
*/
sFromWhere = ldapBase + configurationNamingContext;
deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "pwd");
dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(fsmoRoleOwner=*)";
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("fsmoRoleOwner");
dsLookForDomain.PropertiesToLoad.Add("distinguishedName");
srcDomains = dsLookForDomain.FindAll();
foreach (SearchResult sr in srcDomains)
{
/* For each root look for the groups containing my user
*/
string fsmoRoleOwner = sr.Properties["fsmoRoleOwner"][0].ToString();
string distinguishedName = sr.Properties["distinguishedName"][0].ToString();
Regex srvNameRegEx = new Regex("^CN=NTDS Settings,CN=(.*),CN=Servers,.*$");
Match found = srvNameRegEx.Match(fsmoRoleOwner);
if (distinguishedName.Contains("Partitions"))
Console.WriteLine("Domain Manager is {0}", found.Groups[1].Value);
}
}