Avatar of fruitloopy
fruitloopy

asked on 

VB.NET - Search Active Directory computers description attribute and return computer name

Background explanation:
We have a logon script that updates the Description attribute of the current logged on computer with the username of the user who just logged in. When a user logs in the computer description attribute will change to display the username:
ExampleI am trying to find the correct way to search all active directory computers description field and return the computer name. This will help us to immediately help a user who phones for support. We dont have to waste time asking them for their computer name, we just ask for their username, type that into the app and it will return the computer name which we can remote to.
This is my code so far:
Dim searchRoot As New DirectoryEntry("LDAP://DC=DOMAIN,DC=local")
        Dim searcher As New DirectorySearcher(searchRoot) With { _
            .Filter = "(objectClass=computer)" _
        }

        searcher.PropertiesToLoad.Add("name")
        searcher.PropertiesToLoad.Add("description")

        searcher.Filter = "(&(objectClass=computer))"
        Dim results = searcher.FindAll

        For Each result In results
            If result.Properties.Contains(txtUsernameSc.Text) Then
                txtHostName.Text.ToString(result.GetDirectoryEntry().Properties("name").Value())
            End If



        Next

Open in new window

Nothing is returned though.
Active DirectoryVisual Basic.NET

Avatar of undefined
Last Comment
fruitloopy

8/22/2022 - Mon