2012-05-08 65 views
0

有沒有辦法從活動目錄中使用C#獲取FSMO角色?即使有可能使用LDAP查詢也會起作用。C#從廣告中獲取fsmo角色

很多博客上都有很多VB腳本代碼。但不是C#。

而如何找到一個DC是PDC還是沒有?

感謝

+0

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

回答

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()); 
        } 
       } 
0

你問遠的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); 
    } 
} 
+0

感謝您的回覆。我計算出來並在下面發佈我的解決方案。 – sunder