使用 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 介面來擷取,以列舉屬性。
如需詳細資訊和示範如何擷取所有屬性值的程式代碼範例,請參閱 讀取建構屬性的範例程序代碼。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應