Query Class

The QueryClass class simplifies creating and executing queries against AD. Several steps must be performed to create an AD query: selecting the search root, creating the filter, selecting the attributes, and defining the search scope.

The script typically uses several ActiveX Data Objects (ADOs) to define and execute the query. A Connection object defines a connection to an AD source. A Command object defines, controls, and executes the query itself. The results are returned in a Recordset object. With the QueryClass class the ADO objects are created and controlled within the object itself.

The main script first creates an instance of the QueryClass class. Then the search root, filter, attributes, and search scope are defined by setting their respective properties in the QueryClass. Finally the query is executed by calling the ExecuteQuery function of the object. The results are returned in a Recordset; the main script iterates through the recordset and processes the results.

In the previous chapter we discussed the effect the Command object properties have on the query result set and performance. Those properties are defined in a Dictionary object, which stores the property name as the key. The properties are loaded into the Dictionary object in the C1ass_Initia1ize procedure. You can change these defaults to suit your environment as necessary. To change the properties prior to executing the query, call the SetProperty procedure and pass in the property name and new value.

Two private utility functions appear at the bottom of the script. These functions examine the value entered for the SearchRoot property. The function FormatAdsPath attempts to correctly format the LDAP path entered. The function ADObjectExists checks AD for the existence of the specified search root. These utility functions also appear in other object classes.

Option Explicit


Class QueryClass


Private m_cn, m_cmd Private m_SearchRoot Private m_Fi1terText Private m_Attributes Private m_SearchScope

Private m_CmdProperties


Private Sub C1ass_Initia1ize

Set m_cn = WScript.CreateObject("ADODB.Connection") m_cn.Provider = "ADsDSOObject"

Set m_cmd = WScript.CreateObject("ADODB.Command")

Set m_CmdProperties = WScript.CreateObject("Scripting.Dictionary")

m_CmdProperties.Add m_CmdProperties.Add m_CmdProperties.Add m_CmdProperties.Add m_CmdProperties.Add m_CmdProperties.Add

Asynchronous", False Cache results", True

Chase referra1s",ADS_CHASE_REFERRALS_EXTERNAL Column Names Only", False Deref Aliases", False Page size", 1000

m_CmdProperties.Add "SearchScope", ADS_SCOPE_SUBTREE m_CmdProperties.Add "Size Limit", 0' No limit m_CmdProperties.Add "Sort On", '"" No sort m_CmdProperties.Add "Time Limit", 600 ' 600 seconds = 10 Minutes m_CmdProperties.Add "Timeout", 600

m_SearchScope = "subtree" ' Set a default scope

End Sub

Private Sub C1ass_Terminate m_cn.C1ose

Set m_cn = Nothing

Set m_cmd = Nothing

Set m_CmdProperties = Nothing

End Sub

Public Property Get FilterText

FilterText = m_Fi1terText End Property

Public Property Let FilterText(FilterValue)

m_Fi1terText = FilterValue End Property

Public Property Get Attributes

Attributes = m_Attributes End Property

Public Property Let Attributes(AttributeList)

m_Attributes = AttributeList End Property

Public Property Get Scope

Scope = m_SearchScope End Property

Public Property Let Scope(ScopeText) Select Case UCase(ScopeText)

Case "BASE" : m_SearchScope = "base" Case "ONELEVEL" : m_SearchScope = "onelevel" Case "SUBTREE" : m_SearchScope = "subtree" Case Else

Err.Raise 100, m_ErrSource, "Invalid Scope" m_SearchScope = "subtree"' Set a default scope End Select End Property

Public Property Get SearchRoot

SearchRoot = m_SearchRoot End Property

Public Property Let SearchRoot(AdsPath) If ADObjectExists(AdsPath) Then m_SearchRoot = "<" & FormatAdsPath(AdsPath) & ">" Else

Err.Raise 101, m_ErrSource, "Invalid AdsPath" End If End Property

Public Sub SetProperty(PropertyName,PropertyValue) If m_CmdProperties.Exists(PropertyName) Then m_CmdProperties(PropertyName) = PropertyValue Else

Err.raise 105, m_ErrSource, "Unknown command property" End If End Sub

Public Function ExecuteQuery Dim CmdProperty If IsEmpty(m_SearchRoot) Then

Err.Raise 102, m_ErrSource, "No search root defined" Exit Function End If

If IsEmpty(m_Attributes) Then

Err.Raise 103, m_ErrSource, "No attributes defined" End If m_cn.Open m_cmd.ActiveConnection = m_cn

For Each CmdProperty In m_CmdProperties.Keys m_cmd.Properties(CmdProperty) = m_CmdProperties(CmdProperty)

Next m_cmd.CommandText = m_SearchRoot & ";" & _ m_FilterText & ";" & _ m_Attributes & ";" & _ m_SearchScope Set ExecuteQuery = m_cmd.Execute End Function

Private Function ADObjectExists(ADsPath)

Dim ADObject, LDAPObject

ADSPath = FormatAdsPath(AdsPath)

On Error Resume Next

Set LDAPObject = GetObject("LDAP:")



If Err.Number = 0 Then

ADObjectExists = True Else

ADObjectExists = False End If

Set ADObject = Nothing Set LDAPObject = Nothing End Function

Private Function FormatAdsPath(ADsPath) If Not CBoo1(Instr(ADsPath,"LDAP://")) Then

FormatAdsPath = "LDAP://" & ADsPath Else

FormatAdsPath = AdsPath End If

End Function

Public Property Get m_ErrSource m_ErrSource = "QueryClass" End Property







Public Property Get ADS_SCOPE_BASE

ADS_SCOPE_BASE = 0 End Property

Public Property Get ADS_SCOPE_ONELEVEL


Public Property Get ADS_SCOPE_SUBTREE

ADS_SCOPE_SUBTREE = 2 End Property

Public Property Get ADS_FAST_BIND

ADS_FAST_BIND = &H20 End Property

End Class

Was this article helpful?

0 0
Computer Hard Drive Data Recovery

Computer Hard Drive Data Recovery

Learn How To Recover Your Hard Drive Data After A Computer Failure.

Get My Free Ebook

Post a comment