Here is an example using VBScript. Note that this example only searches the default domain of an Active Directory.
1 optionexplicit
2
3 dim oRoot
4 dim oConnect, oCommand, rs
5 dim strConfigurationContext
6 dim oGAL
7 dim strGalContainer
8 dim strSearchRoot
9 dim entry
10 dim oSearchRoot
11
12 'to get LDAP namespace
13 set oRoot = GetObject("LDAP://RootDSE")
14 strConfigurationContext = oRoot.Get("configurationNamingContext")
15
16 'Open the global address list container
17 strGalContainer = "LDAP://CN=All Global Address Lists,CN=Address Lists Container,CN=Contoso,CN=Microsoft Exchange,CN=Services, " & strConfigurationContext
18
19 ' Iterate through global address lists. In this example, only the last one is used.
20 foreach entry in GetObject(strGalContainer)
21 set oGAL = entry
22 next
23
24 'Get the searchroot
25 strSearchRoot = "LDAP://" & oRoot.Get("defaultNamingContext")
26
27 'Using ADO to query the Active Directory for a particule user
28 set oConnect = CreateObject("ADODB.Connection")
29 set oCommand = CreateObject("ADODB.Command")
30
31 'Open the connection
32 oConnect.Provider = "ADsDSOObject"
33 oConnect.Open "Active Directory Provider"
34
35 'Create a command object for this connection
36 Set oCommand.ActiveConnection = oConnect
37 oCommand.CommandText = "SELECT samAccountName, givenName, sn FROM '" & strSearchRoot & "' WHERE msExchHideFromAddressLists <> true AND showInAddressBook = '" & oGal.Get("distinguishedName") & "' ORDER BY sAMAccountName ASC"
38
39 'Create a recordset based on the ADO command
40 set rs = oCommand.Execute
41
42 while (not rs.EOF)
43 wscript.echo rs.Fields("samAccountName") & " (" & rs.Fields("givenName") & " " & rs.Fields("sn") & ")"
44 rs.MoveNext
45 wend