2012-05-08 6 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のActiveDirectoryRoleロール) { 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

は、Active-Directoryからファイブ柔軟なシングルマスタ操作(FSMO)の役割を見つけるために、これまでのLDAP道を尋ねます。これらは、Active Directoryの異なる名前付けコンテキストで属性fsmoRoleOwnerを検索することで検出できます。 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); 
    } 
} 
+0

私はそれを理解し、以下に私の解決策を掲載しました。 – sunder

関連する問題