Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying to search for computer names in my Active directory and display the results in a list box once the user selects the correct room number the computers are in from a combo box. I have setup my dirSearcher.Filter to only display computers in my list box with the following code below and all the computers names display correctly.
<br />
dirSearcher.Filter = ("(objectClass=Computer)")<br />

But when I add the other part to my filter which allows the user to select the room number from a combo box and all the computers that contain the room# in the computer name to display my search returns no results I would like help if possible for someone to point me in the right direction as the correct way to filter by computer name and display the results in the listbox. The code I have is below any help is appreciated.
VB
Dim compuDesc As String
compuDesc = ComboBox1.Text
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
dirSearcher.Filter = "(&(objectClass=Computer)(name = RM131A*))"
dirSearcher.PropertiesToLoad.Add("name")
Dim dirSearchResults As SearchResult
For Each dirSearchResults In dirSearcher.FindAll()
    ListView1.Items.Add(dirSearchResults.GetDirectoryEntry().Name.ToString())
Next
Posted
Updated 10-Mar-11 2:57am
v4
Comments
postonoh 11-Feb-11 16:33pm    
can you give code full code with the search filter
William Winner 15-Feb-11 16:59pm    
have you tried it without the single quotes? as in

"(&(objectClass=computer)(cn={0})"
tupacIT 16-Feb-11 10:28am    
Mo John Thanks I have updated the questioin with my full block of code and William thanks for your help I tried your suggestion without the single quotes and I am still getting the same error also any additional help is appreciated.

Without seeing the rest of the Active Directory code that you have written I would suggest this article maybe able to help you

Active Directory [^]
 
Share this answer
 
Comments
Sandeep Mewara 11-Feb-11 23:58pm    
Nice link and article directed to! 5!
sir, you can use String.Fomat just like:

VB
dirSearcher.Filter = String.Fomat("(objectClass='Computer') and (cn ='{0}')", compuDesc )
 
Share this answer
 
v2
Comments
tupacIT 15-Feb-11 15:35pm    
Ali,
Thanks when I execute the code I get the error message "The (&(objectClass=Computer)(cn = 'RM212') search filter is invalid." Any additional help is appreciated. Thanks
Ali Al Omairi(Abu AlHassan) 16-Feb-11 4:42am    
i think the key word 'and' can solve the issue. :)
tupacIT 16-Feb-11 10:28am    
Ali, Thanks I have removed the 'and' I am still getting the error
Ali Al Omairi(Abu AlHassan) 17-Feb-11 3:43am    
Sir, you shouldn't remove 'and', you should add it; the result should be :

dirSearcher.Filter = String.Fomat("(objectClass='Computer') and (cn ='{0}')", compuDesc ) '(objectClass='Computer') and (cn ='RM212')
tupacIT 17-Feb-11 14:47pm    
Ali thanks for your help this is what I got below and now I get a 'Directory Services is Unavailable' error message
dirSearcher.Filter = String.Format("(objectClass=Computer)and(cn = {0})", compuDesc)
I am no VB guru, but does it have anything like string.Contains?
 
Share this answer
 
Comments
tupacIT 15-Feb-11 15:35pm    
Not sure what you mean. Thanks !!
I finally figured it out the answer is below.

VB
Dim compuDesc As String
        compuDesc = ComboBox1.Text
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        dirSearcher.Filter = ("(objectClass=Computer)")
        Dim dirSearchResults As SearchResult
        Dim computername As String
        ' 3. Loop through all the computer names returned
        For Each dirSearchResults In dirSearcher.FindAll()
            computername = dirSearchResults.GetDirectoryEntry().Name

            If (computername.Contains(compuDesc)) Then
                computername = computername.Remove(0, 3)
                ListView1.Items.Add(computername)
            End If
        Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900