如何使用C#从Active Directory中删除计算机账户

4

有没有使用C#删除Active Directory计算机帐户的示例?

我搜索了许多来源,但都是关于用户帐户的。

在此处添加我的代码,由于某些原因我总是得到错误。

public static bool checkExistingPC(string compName,string userName,string userPwd )
{
    try
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://test.com",userName,userPwd,AuthenticationTypes.Secure);
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
       mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + compName + ")(dn=" + compName + ")))";
       foreach (SearchResult result in mySearcher.FindAll())
       {
           if (result != null)
           {

               MessageBox.Show("computer GetDirectoryEntry():" + result.Path+"\n"+"computer path: "+result.Path);
                DirectoryEntry entryToRemove = new DirectoryEntry(result.Path,userName,userPwd);
                 entry.Children.Remove(entryToRemove);

               return true;
           }
           else
           {
               return false;
           }
       }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    return false;
}

@Oli:Active Directory 是微软的Windows目录服务。详情请见http://zh.wikipedia.org/wiki/Active_Directory。 - kaveman
你尝试过 entryToRemove.DeleteTree(); 吗? - Jeremy
在执行DeleteTree后,请尝试entryToRemove.CommitChanges(); 确保您只删除计算机条目而不是其他内容:D - Jeremy
据我了解,DeleteTree将删除对象容器内的任何内容...我还没有尝试过,因为我觉得这有点冒险。 - ikel
4个回答

5
如果您使用的是.NET 3.5及更高版本(如果不是-那是升级的时候了!),您应该查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在此处阅读所有相关信息: 基本上,您可以定义域上下文并轻松地在AD中查找用户和/或组:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find the computer in question
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(ctx, "NAME");

// if found - delete it
if (computer != null)
{
   computer.Delete();
}

新的S.DS.AM使得在AD中玩弄用户、计算机和组变得非常容易!

如果需要不同的凭据,可以使用用户名/密码创建PrincipalContext。例如:new PrincipalContext(ContextType.Domain, "domain.net", "DC=domain,DC=net", ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing, "domain\\username", "mySecretPassword") - Daniel Müller

4

使用位于System.DirectoryServices下的ADSI,使用提交机制,这里是一个可工作的示例:

/* Retreiving RootDSE infos
 */
string ldapBase = "LDAP://WM2008R2ENT:389/";
string sFromWhere = ldapBase + "rootDSE";
DirectoryEntry root = new DirectoryEntry(sFromWhere, "dom\\jpb", "PWD");
string defaultNamingContext = root.Properties["defaultNamingContext"][0].ToString();

/* Retreiving the computer to remove
 */
sFromWhere = ldapBase + defaultNamingContext;
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", ".biènèsph^r^.1966");

DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(&(cn=MACHSUPR))"; // MACHSUPR is the computer to delete
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("cn");
dsLookForDomain.PropertiesToLoad.Add("distinguishedName");

SearchResultCollection srcComputer = dsLookForDomain.FindAll();

foreach (SearchResult aComputer in srcComputer)
{
  /* For each computer
   */
  DirectoryEntry computerToDel = aComputer.GetDirectoryEntry();
  computerToDel.DeleteTree();
  computerToDel.CommitChanges();
}

谢谢,根据您提供的代码,我成功让它工作了。 - ikel

0

使用 WMI 和/或 System.DirectoryServices 命名空间 (http://msdn.microsoft.com/en-us/library/system.directoryservices.aspx)。


请查看:http://msdn.microsoft.com/zh-cn/library/system.directoryservices.accountmanagement.computerprincipal.aspx - Jeremy

-1

这可能不是您要寻找的完全内容,但此网站提供了许多与C#中的AD相关的代码示例,包括删除安全组和将用户从组中移除。


谢谢,但我发现那个网站没有关于计算机账户的信息。 - ikel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接