How can I search an entire active directory via ADSI using vbscript?

How can I search an entire active directory via ADSI using vbscript?

Post by Tulio Quinone » Fri, 29 Mar 2002 05:49:47



sorry for the repost.

I have the user or contacts email address and would like to find the object
it belongs to via ADSI using vbscript.

Thanks

 
 
 

How can I search an entire active directory via ADSI using vbscript?

Post by Jerry Rasmusse » Fri, 29 Mar 2002 22:50:26


It is something like this.  Soryy I do not have time to clean it up I am in
a rush.
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Provider = "ADsDSOObject"  'The ADSI OLE-DB provider

oConnection.Open "ADs Provider"
strQuery = "<" & strADsPath &
">;(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & * &
"));sAMAccountName,cn,distinguishedName,Mail,givenName;subtree"
Set oRecordset = oConnection.Execute(strQuery)

If oRecordset.EOF And oRecordset.BOF Then
    wscript.echo oRecordSet.Fields("Mail")
end if
  MsgBox "No duplicate UPN found"

Quote:> sorry for the repost.

> I have the user or contacts email address and would like to find the
object
> it belongs to via ADSI using vbscript.

> Thanks


 
 
 

How can I search an entire active directory via ADSI using vbscript?

Post by MVP for II » Sat, 30 Mar 2002 17:52:44


this is a copy-paste from the MSDN. Also accesable for free at
http://msdn.microsoft.com

The following subroutine performs a query against a specified root object
and display all groups and groups by type:

'************************************
'  QueryAndOutputGroups()   -           Performs several sample searches
using ADO
'
'
'    Parameters
'
'        oDirObjectRoot As IDirectoryObject - The root from where searches
will be performed
'
Sub QueryAndOutputGroups(oDirObjectRoot As IDirectoryObject)

    '*******************************
    ' Search for all the groups
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL GROUPS>>>>>>>>>>>>>>>>>" 'cn =
'UG*' objectCategory
    SimpleDirectorySearch oDirObjectRoot, "objectCategory = 'group'",
ADS_SCOPE_SUBTREE

    '*****************************************
    ' Search for all the GLOBAL groups
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all the
GLOBAL groups"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL GLOBAL Groups>>>>>>>>>>>>>>>>>"
    SimpleDirectorySearch oDirObjectRoot, "GroupType=" &
Str(ADS_GROUP_TYPE_GLOBAL_GROUP) & " or " & _
                            "GroupType=" & Str(ADS_GROUP_TYPE_GLOBAL_GROUP
Or ADS_GROUP_TYPE_SECURITY_ENABLED), _
                            ADS_SCOPE_SUBTREE

    '*****************************************
    ' Search for all the DOMAIN LOCAL  groups
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all the
DOMAIN LOCAL groups"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Domain Local
Groups>>>>>>>>>>>>>>>>>"
    SimpleDirectorySearch oDirObjectRoot, "GroupType=" &
Str(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP) & " or " & _
                            "GroupType=" &
Str(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED), _
                            ADS_SCOPE_SUBTREE

    '        #ifdef USE_UNIVERSAL_GROUPS

    '*****************************************
    ' Search for all the UNIVERSAL  groups
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all the
UNIVERSAL groups"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Universal
Groups>>>>>>>>>>>>>>>>>"
    SimpleDirectorySearch oDirObjectRoot, "GroupType=" &
Str(ADS_GROUP_TYPE_UNIVERSAL_GROUP) & " or " & _
                            "GroupType=" &
Str(ADS_GROUP_TYPE_UNIVERSAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED), _
                            ADS_SCOPE_SUBTREE
    '        #End If

    '*****************************************
    ' Search for all the Security Enabled  groups
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all
Security Enabled groups"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Security Enabled
Groups>>>>>>>>>>>>>>>>>"
    SimpleDirectorySearch oDirObjectRoot, "GroupType=" &
Str(ADS_GROUP_TYPE_SECURITY_ENABLED) & " or " & _
                            "GroupType=" &
Str(ADS_GROUP_TYPE_SECURITY_ENABLED Or ADS_GROUP_TYPE_SECURITY_ENABLED), _
                            ADS_SCOPE_SUBTREE

    '*****************************************
    ' Search for all the Security Enabled LOCAL groups
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all
Security Enabled LOCAL groups"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Security Enabled Domain Local
Groups>>>>>>>>>>>>>>>>>"
    SimpleDirectorySearch oDirObjectRoot, "GroupType=" &
Str(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP Or _
                            ADS_GROUP_TYPE_SECURITY_ENABLED), _
                            ADS_SCOPE_SUBTREE

    '*****************************************
    ' Search for all the DOMAIN LOCAL groups whose
    ' name begins with 'UGExercise'
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of all DOMAIN
LOCAL  groups that begin with the text 'UGExercise'"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Domain Local Groups Beginnig
with 'UGExercise' >>>>>>>>>>>>>>>>>"

    SimpleDirectorySearch oDirObjectRoot, "cn='UGExercise*' and (GroupType="
& _
                            Str(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP) & " or
GroupType=" & _
                            Str(ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP Or
ADS_GROUP_TYPE_SECURITY_ENABLED) & _
                            ")", ADS_SCOPE_SUBTREE

    '*****************************************
    ' Search for ALL the groups whose
    ' name begins with 'UGExercise'
    DisplayMessage " "
    DisplayMsgWaitForInput "Hit OK to perform a subtree search of ALL groups
that begin with the text 'UGExercise '"
    DisplayMessage " "
    DisplayMessage "<<<<<<<<<<<<<<<<<<<<<ALL Groups Beginning with
'UGExercise' >>>>>>>>>>>>>>>>>"

    SimpleDirectorySearch oDirObjectRoot, " cn='UGExercise*' and
objectCategory='group'", ADS_SCOPE_SUBTREE

End Sub

'///////////////////////////////////////////////////////////////////////////
/////////////////////////
'   SimpleDirectorySearch()   - Uses ADO to search the directory
'                               Results are displayed in the list box
'    Parameters
'
'        oDirObjectRoot As IDirectoryObject  - Root of the search
'        ByVal sSearchFilter As String       - LDAP Search filter
'        ByVal lScope As Integer             - Scope for Searching possible
values are:
'                                                ADS_SCOPE_BASE
'                                                ADS_SCOPE_ONELEVEL
'                                                ADS_SCOPE_SUBTREE
'
'        (See IDirectorySearch and ADS_SCOPEENUM in the docs for more info)
'
Sub SimpleDirectorySearch(oDirObjectRoot As IDirectoryObject, ByVal
sSearchFilter As String, ByVal lScope As Integer)

    Dim iIndex As Integer
    iIndex = 0
    Dim v, j, i

    Dim con As New Connection, rs As New Recordset
    Dim Com As New Command
    Dim oIADs As IADs
    Dim sAdsPathRoot As String

    ' Get the LDAP path to the passed in object
    sAdsPathRoot = GetAdsPath(oDirObjectRoot)

    'Open a Connection object
    con.Provider = "ADsDSOObject"

    '-----------------------------------------------------------------
    ' To be authenticated using alternate credentials
    ' use connection properties of User ID and Password
    '-----------------------------------------------------------------
    ' con.Properties("User ID") = "Administrator"
    ' con.Properties("Password") = ""

    ' Open the connection
    con.Open "Active Directory Provider"

    ' Create a command object on this connection
    Set Com.ActiveConnection = con

    ' set the query string using SQL Dialect
    Com.CommandText = "select name,AdsPath from '" & sAdsPathRoot & "' where
" & sSearchFilter & " ORDER BY NAME"

    ' Tell the user what the search filter is
    DisplayMessage "Search Filter = " & Com.CommandText

    '---------------------------------------------------
    ' Or you can use LDAP Dialect, for example,
    '---------------------------------------------------
    ' Ex Com.CommandText =
"<LDAP://Microsoft1/dc=Microsoft,DC=com>;(objectClass=*);name"
    ' For LDAP Dialect, the valid search scope are base, oneLevel and
subtree
    ' Com.CommandText = "<" & adDomainPath &
">;(objectClass=*);name;subtree"
    ' For LDAP Dialect (<LDAP:...>), there is no way to specify sort order
in the string,
    ' However, you can use this SORT ON property to specify sort order.
    ' for SQL Dialect you can use ORDER BY in the SQL Statement
    ' Ex. Com.Properties("Sort On") = "Name"

    'Set the preferences for Search
    Com.Properties("Page Size") = 1000
    Com.Properties("Timeout") = 30 'seconds
    Com.Properties("searchscope") = lScope
    Com.Properties("Chase referrals") = ADS_CHASE_REFERRALS_EXTERNAL
    Com.Properties("Cache Results") = False ' do not cache the result, it
results in less memory requirements

    'Execute the query
    Set rs = Com.Execute

    ' Tell the user how many rows
    DisplayMessage "Returned " & Str(rs.RecordCount) & " rows"

    ' Navigate the record set
    If Not rs.EOF Then
        rs.MoveFirst
    End If

    On Error Resume Next
    While Not rs.EOF
        ' Display the LDAP path for the row
        DisplayMessage rs.Fields("AdsPath")
        rs.MoveNext
    Wend

End Sub

--
The scalable ASP Session replacement for webfarms
http://www.nieropwebconsult.nl/asp_session_manager.htm

"Tulio Quinones" <Tulio_Quino...@nyc.yr.com> wrote in message

news:OMg0ADd1BHA.1400@tkmsftngp04...
> sorry for the repost.

> I have the user or contacts email address and would like to find the
object
> it belongs to via ADSI using vbscript.

> Thanks