?
Solved

how to delete computer account from AD using c#

Posted on 2011-10-26
6
Medium Priority
?
762 Views
Last Modified: 2013-12-16
can someone give me a sample c# code on how to delete a computer from AD?

thanks
0
Comment
Question by:Ikelca
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:Victor Tarasov
ID: 37035759
Connect LDAP to AD and remove object called as computer name.
0
 

Author Comment

by:Ikelca
ID: 37035806
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
 
LVL 4

Expert Comment

by:Victor Tarasov
ID: 37035933
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Ikelca
ID: 37036138
how can we convert this to c#??
0
 
LVL 4

Accepted Solution

by:
Victor Tarasov earned 2000 total points
ID: 37036338
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
 

Author Comment

by:Ikelca
ID: 37038222
some minor issues, but i managed to get over
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question