Anyone can use Active Directory Users and Computers to search fora user. Just right-click the container you want to search, and select the find option. Easy-peasy, lemon squeezy.
But how can you programmatically search Active Directory? It's pretty straightforward, actually. This VBScript sample will show you a couple of different basic concepts of working with AD.
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
' *** change these values to search for a different user, or
' *** search in a different domain.
' *** samAccountName = username to be searched for
' *** MyDomainName = Domain to be searched
NameToSearchFor = "MyUserName"
MyDomainName = "dc=mydomain,dc=com"
' *** Make objects
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
' *** Make connection to Active Directory and execute
objConnection.Provider = ("ADsDSOObject")
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT samAccountName FROM " & _
"'LDAP://" & MyDomainName & "' " & _
"WHERE samAccountName = '" & MyUserName & "'"
objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
' *** output the results to the screen
If objRecordSet.RecordCount = 0 Then
Wscript.Echo MyUserName & " is not in use."
Wscript.Echo MyUserName & " is being used."
No comments on this post yet!
You must be logged in to post a comment.