首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#从广告中获得fsmo角色

C#从广告中获得fsmo角色
EN

Stack Overflow用户
提问于 2012-05-08 06:39:51
回答 2查看 2.2K关注 0票数 0

有没有办法从活动目录中获得使用C#的FSMO角色?,即使可能的话,使用LDAP查询也能工作。

在很多博客上都有大量的VB脚本代码。但不是C#。

和如何找到DC是PDC还是不PDC?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-08 07:10:52

我原以为很难找到这份工作,但最终还是很容易的。我正在张贴代码,以防将来有人需要这个。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-05-08 20:07:10

您可以通过使用LDAP的方法从Active Directory找到5个灵活的单一主操作(FSMO)角色。可以在Active的不同命名上下文中找到它们寻找属性fsmoRoleOwner。您可以在这里找到用C#编写的3条LDAP搜索。小心,代码不那么干净,这只是概念的一种证明。

确定FSMO角色持有者是一个很好的来源。

代码语言:javascript
复制
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);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10493831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档