Hello,
I have wasted a whole day on this seemingly trivial task.... :-(
I'm trying to delete a specific account from AD. I have been given an account to use which has special privilages to delete computer accounts. I have searched for a .NET example of this task, but I can only find VBS examples. I must use the DirectoryServices classes as I want to pass the AD account credentials.
Here is my code:
Dim entry As New DirectoryEntry("LDAP://dc=company,dc=com,dc=au", "company\special_account", "password", AuthenticationTypes.Secure) Dim pcdir As New DirectorySearcher("(&(objectClass=Computer)(objectCategory=Computer)(cn=robspc))")Dim usersr As SearchResultpcdir.SearchRoot = entry
usersr = pcdir.FindOne
Dim dn As String = usersr.Properties("distinguishedName")(0) Dim tt As DirectoryEntrytt =
New DirectoryEntry("LDAP://" & dn, "company\special_account", "password", AuthenticationTypes.Secure) Tryentry.Children.Remove(tt)
Catch ex As Exception End Try The variable dn = "CN=robspc,OU=Workstations,OU=SiteX,OU=Org,DC=company,DC=com,DC=au"The following error is spat out....when entry.children.remove(tt) is run: "There is no such object on the server. (Exception from HRESULT: 0x80072030)"
Any assistance would be greatly appreciated.
Thanks.
Rob.

Deleting Computer accounts from AD with System.DirectoryServices
hazz
The method that you are using is not working because the object retrived from the search is not a direct child of the object you are using as a parent. There are two methods to delete an item retrived from a search. One, when the object is retrived you can call DeleteTree on the DirectoryEntry which will delete the object plus all of it's children. Second, you can also retrive the parent of object by calling Parent and then using this object you can call Children.Remove and pass the original object.
To use the first method replace
entry.Children.Remove(tt)
with
tt.DeleteTree();
Travis Querec[MSFT]