有没有办法从活动目录中使用C#获取FSMO角色?即使有可能使用LDAP查询也会起作用。C#从广告中获取fsmo角色
很多博客上都有很多VB脚本代码。但不是C#。
而如何找到一个DC是PDC还是没有?
感谢
有没有办法从活动目录中使用C#获取FSMO角色?即使有可能使用LDAP查询也会起作用。C#从广告中获取fsmo角色
很多博客上都有很多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的方式找到击掌灵活单主机操作(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);
}
}
感谢您的回复。我计算出来并在下面发布我的解决方案。 – sunder
System.DirectoryServices.ActiveDirectory.Domain dom = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain(); System.DirectoryServices.ActiveDirectory.DomainController pdcdc = dom.PdcRoleOwner; foreach(pdcdc.Roles中的ActiveDirectory角色) Console.WriteLine(“{0}”,role.ToString()); } 上面的代码会给我fsmo角色类型,但不会告诉它是否设置。 – sunder