Filter Results (VBScript)

This code can be found in Chapter 22 of Active Directory, 3rd Edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Active Directory, Third Edition" 
' ISBN: 0-596-10173-2

Option Explicit
   
Const adStateOpen = 1
   
Dim objFileSystem 'A FileSystemObject
Dim objOutput     'A TextStream Object
Dim objConn       'An ADO Connection object
Dim objRS         'An ADO Recordset object
Dim intCount      'An integer
   
'*****************************************************************************
'Create the file if it doesn't exist or truncate it if it does exist
'*****************************************************************************
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFileSystem.CreateTextFile("c:\out.txt", TRUE)
   
'*****************************************************************************
'Write out the current time and date using the VBScript 'Now' function
'*****************************************************************************
objOutput.WriteLine "Starting..." & Now
   
Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADSDSOObject"
objConn.Open "", "CN=Administrator,CN=Users,dc=mycorp,dc=com", ""
If objConn.State = adStateOpen Then
  objOutput.WriteLine "Authentication Successful!"
Else
  objOutput.WriteLine "Authentication Failed."
  WScript.Quit(1)
End If
   
Set objRS = objConn.Execute _
  ("<LDAP://dc=mycorp,dc=com>;(&(objectClass=user)(objectCategory=person)); "_
   & "cn;SubTree")
   
'*****************************************************************************
'Loop through the ASCII characters letters Asc("a") to Asc("z")
'where Asc("a") = 97 and Chr(97) = "a"
'*****************************************************************************
For intCount = 97 To 122
  objRS.Filter = "cn LIKE '" & Chr(intCount) & "*'"
  objOutput.WriteLine(Chr(intCount) & " = " & objRS.RecordCount)
Next
   
objConn.Close
Set objRS = Nothing
   
objOutput.Close

This code has been viewed 2183 times.

New from the creators of TechTasks.com: StatSheet.com