有没有办法在Active Directory中使用C#获取FSMO角色?即使可能使用LDAP查询也可以。
许多博客都提供了很多VB脚本代码。但不是C#。
如何找到DC是否是PDC?
由于
答案 0 :(得分:1)
我认为找到这个很难,但最终会很容易。我发布代码以防将来有人需要这个。
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());
}
}
答案 1 :(得分:0)
您要求远程LDAP方法从Active-Directory中找到五个灵活单主机操作(FSMO)角色。可以找到它们在Active Directory的不同命名上下文中查找属性fsmoRoleOwner
。您将在此处找到3个用C#编写的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);
}
}