Get the creation date for users from Active Directory

This sample connects to active directory and retrieves the name and createTimeStamp
field for every user in a given OU or Container (and subcontainers).  You will
need to change the ContainerPath variable to an appropriate value for your domain.
If you are binding to the builtin users container in Active Directory use a path like:

LDAP://CN=Users,DC=subdomain,DC=domain,DC=com

If you are binding to an OU, use a path like

LDAP://OU=Employees,DC=subdomain,DC=domain,DC=com

Imports System.DirectoryServices

Private Sub GetCreationDate()
Dim ContainerPath As String = “LDAP://CN=Users,DC=subdomain,DC=domain,DC=com”

Console.WriteLine(“Start”)
Dim searcher As DirectorySearcher
Dim results As SearchResultCollection
Dim searchRoot As New DirectoryEntry(ContainerPath)
Try

searcher = New DirectorySearcher(searchRoot)
searcher.PropertiesToLoad.Add(“createTimeStamp”)
searcher.PropertiesToLoad.Add(“cn”)
searcher.Filter = “(&(objectClass=user)(objectCategory=person))”

results = searcher.FindAll

Console.WriteLine(“Users
Found: “
& results.Count)


These two variables will hold the values found


Dim createTime As String = “”

Dim cname As String = “”


Loop through each entry in the result collection and



write the name and createTimeStamp value


For Each result As SearchResult In results

Get the value currently stored for createTimeStamp


createTime = result.Properties(“createTimeStamp”)(0)


Get the value currently stored for name


cname = result.Properties(“cn”).Item(0).ToString

Console.WriteLine(cname.PadLeft(40) & ControlChars.Tab &
createTime.PadLeft(12))
Next

Console.WriteLine(“Complete”)
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(“Error:
& ex.Message)
End Try

End Sub

Random Posts

Loading…

Leave a Reply