2017-04-27 4 views
0

ドメイングループにホスト名を追加しようとしましたが、例外There is a naming violationが発生しています。C#ADドメイングループにPCを追加する

私はこの正確なことをカバーするいくつかの受け入れられた回答を見てきました。

try 
{ 
    DirectoryEntry de = new DirectoryEntry("LDAP://aa.bbbbb.com/CN=Group,OU=Application,OU=Groups,OU=US,DC=aa,DC=bbbbb,DC=com"); 
    string hostname = "CN=" + SystemInformation.ComputerName; 
    DirectoryEntry add = de.Children.Add(hostname, "Computer"); 
    add.CommitChanges(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Group join failed" + Environment.NewLine + Environment.NewLine + ex.ToString()); 
} 

ご協力いただければ幸いです。

答えて

0

私は問題が何かを理解しました - ホスト名ではなく識別名を渡す必要がありました...実際にMSDNのドキュメントを読んでいれば分かりました...また、de.Children.Add()これを達成するための有効な方法(これは.Net 3.5 IIRCのためにSE上で受け入れられた回答でした)、それを達成するためにde.Properties["member"].Add()を使用しました。

そこにどんなのGooglerのため

更新されたソースコード:

private void DoStuff(object sender, EventArgs e) 
{ 
    using (Process addgroup = new Process()) 
    { 
     string hostname = Environment.MachineName; 
     AddMemberToGroup("LDAP://aa.bbbbb.com/CN=Group,OU=Application,OU=Group,OU=US,DC=aa,DC=bbbbb,DC=com", hostname); 
    } 
} 

private void AddMemberToGroup(string ldapString, string host) 
{ 
    try 
    { 
     DirectoryEntry de = new DirectoryEntry(ldapString); 
     string distHost = GetDistinguishedName(host); 
     if (!String.IsNullOrEmpty(distHost)) 
     { 
      de.Properties["member"].Add(distHost); 
      de.CommitChanges(); 
     } 
     else 
     { 
      MessageBox.Show("Distinguished Host Name returned NULL"); 
     } 
    } 
    catch(Exception ex) 
    { 
     MessageBox.Show("Group join failed" + Environment.NewLine + Environment.NewLine + ex.ToString()); 
    } 
} 

private string GetDistinguishedName(string compName) 
{ 
    try 
    { 
     PrincipalContext pContext = new PrincipalContext(ContextType.Domain); 
     ComputerPrincipal compPrincipal = ComputerPrincipal.FindByIdentity(pContext, compName); 
     return compPrincipal.DistinguishedName; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Failed to get the Distinguished Hostname from Active Directory" + Environment.NewLine + Environment.NewLine + ex.ToString()); 
     return null; 
    } 
} 
関連する問題