fortypoundhead.com

VBScript: Search Active Directory for User

Posted On 2012-03-18 by dwirch
Keywords:
Tags: Active Directory VBScript Scripting Windows Windows Server 2008 Windows Server 2003
Views: 12483


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.

  • Define search parameters
  • Define ADO databjects for the connections
  • Open a connection to Active Directory
  • Perform an LDAP query
  • Process the results

This code sample may be pretty basic to some folks in the IT world. But remember, there are those that are just starting their journey into the world of technology, and might need a bit of help to get rolling. As such, I've commented the code quite a bit, in order to help understanding of what is going on.

Feel free to ask questions, though. That's why the site is here - answer your questions!

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."

Else

Wscript.Echo MyUserName & " is being used."

End If


About the Author

dwirch has posted a total of 173 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

By: AnonymousCoward
Date: 2015-07-06

Thanks very much.  this saved my from many hours of headache.  I'm new to Active Directory and this worked perfectly.  Thanks again.


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.198.233.27

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers

When setting up a certificate authority ...
dwirch posted on July 13, 2017 at about 9:07 in General