使用 GetInfoEx 優化
IADs.GetInfoEx 方法可用來從基礎目錄服務將特定屬性值載入本機快取。 這個方法只會將指定的屬性值載入本機快取。 IADs.GetInfo 方法可用來將所有屬性值載入本機快取。
IADs.GetInfoEx 方法會從基礎目錄存放區取得 Active Directory 物件屬性的特定目前值,並重新整理快取的值。
不過,如果屬性快取中已有值,則呼叫 IADs.Get 或 IADs.GetEx 方法,而不先呼叫 IADs.GetInfoEx,該屬性會擷取快取的值,而不是基礎目錄中最新的值。 在本機快取已被修改的情況下,這可能會導致更新的屬性值遭到覆寫,但這些值尚未使用呼叫 IADs.SetInfo 方法的方式認可至基礎目錄服務。 避免快取問題的建議方法是,應該在呼叫 IADs.GetInfo之前,始終透過呼叫 IADs.SetInfo 來認可屬性值變更。
Dim usr As IADs
Dim PropArray As Variant
On Error GoTo Cleanup
' Bind to a specific user object.
Set usr = GetObject("LDAP://CN=Jeff Smith,CN=Users,DC=fabrikam,DC=com")
' The code example assumes that the property description has a single value in the directory.
' Be aware that this will implicitly call GetInfo because, at this point, GetInfo
' has not yet been called (implicitly or explicitly) on the usr object.
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
' Change two of the attribute values in the local cache.
usr.Put "cn", "Jeff Smith"
usr.Put "title", "Vice President"
usr.Put "department", "Head Office"
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
' Initialize the array of properties to pass to GetInfoEx.
PropArray = Array("department", "title")
' Get the specified attribute values.
usr.GetInfoEx PropArray, 0
' The specific attributes values were overwritten, but the attribute
' value not retrieved has not changed.
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
Cleanup:
If (Err.Number<>0) Then
MsgBox("An error has occurred. " & Err.Number)
End If
Set usr = Nothing
擷取 Active Directory 建構屬性
在 Active Directory 中,當呼叫 IADs.GetInfo 方法時,會擷取和快取大部分建構的屬性(如果快取是空的,IADs.Get 會執行隱含 IADs.GetInfo 呼叫時)。 不過,某些建構的屬性不會自動擷取和快取,因此,要求明確呼叫 IADs.GetInfoEx 方法來擷取它們。 例如,在 Active Directory 中,呼叫 IADs.GetInfo 方法時,不會擷取 canonicalName 屬性,IADs.Get 會傳回 E_ADS_PROPERTY_NOT_FOUND。 必須呼叫 IADs.GetInfoEx 方法來擷取 canonicalName 属性。 這些相同的建構屬性也不會使用 IADsPropertyList 介面來擷取,以列舉屬性。
如需詳細資訊和示範如何擷取所有屬性值的程式代碼範例,請參閱 讀取建構屬性的範例程式代碼。