• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

how to delete computer account from AD using c#

can someone give me a sample c# code on how to delete a computer from AD?

thanks
0
Ikelca
Asked:
Ikelca
  • 3
  • 3
1 Solution
 
Victor TarasovHead of the Department of System AdministrationCommented:
Connect LDAP to AD and remove object called as computer name.
0
 
IkelcaAuthor Commented:
well, any sample code?
i know i need to bind ldap, but my code always have error, therefore im looking of some sample code.
however, i cannot find c# sample for deleting computer from AD anywhere....
0
 
Victor TarasovHead of the Department of System AdministrationCommented:
at VB.NET
Sample code(VB.NET) search Computer objects in OU, and return list computer names.
    Private Function getComputersInOU(ByVal ou As String) As String
        Dim de As New DirectoryEntry(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ADConnStr").ConnectionString)
        Dim User As New DirectoryEntry
        Dim tmp As String = ""
        Try
            de = de.Children.Find("OU=" & ou)
            Dim search As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(de)
            search.Filter = "(&(objectClass=computer)(objectCategory=Computer))"
            Dim result As DirectoryServices.SearchResultCollection = search.FindAll
            If (result Is Nothing) Then Return ""
            For Each use As DirectoryServices.SearchResult In result
                For Each prop As System.Collections.DictionaryEntry In use.Properties
                    If prop.Key = "samaccountname" Then
                        tmp &= prop.Value(0) & ","
                    End If
                Next
            Next
            Return tmp
        Catch
            Return ""
        End Try

    End Function

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
IkelcaAuthor Commented:
how can we convert this to c#??
0
 
Victor TarasovHead of the Department of System AdministrationCommented:
http://www.google.com/search?q=convert+code+from+vb.net+to+c%23&ie=utf-8&oe=utf-8&aq=t
private string getComputersInOU(string ou)
{
	DirectoryEntry de = new DirectoryEntry(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ADConnStr").ConnectionString);
	DirectoryEntry User = new DirectoryEntry();
	string tmp = "";
	try {
		de = de.Children.Find("OU=" + ou);
		DirectoryServices.DirectorySearcher search = new DirectoryServices.DirectorySearcher(de);
		search.Filter = "(&(objectClass=computer)(objectCategory=Computer))";
		DirectoryServices.SearchResultCollection result = search.FindAll;
		if ((result == null))
			return "";
		foreach (DirectoryServices.SearchResult use in result) {
			foreach (System.Collections.DictionaryEntry prop in use.Properties) {
				if (prop.Key == "samaccountname") {
					tmp += prop.Value(0) + ",";
				}
			}
		}
		return tmp;
	} catch {
		return "";
	}

}

Open in new window

0
 
IkelcaAuthor Commented:
some minor issues, but i managed to get over
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now