我已登录DC=domain,dc=company1,dc=com
我正在使用VBA查询AD。我可以毫无问题地查询GC://dc=company1,dc=com
。但是当我尝试查询GC://dc=company2,dc=com
时,我没有得到任何结果。看来我的代码不会追逐推介。
我可以使用PowerShell运行查询,所以我知道它应该可行。我只是无法弄清楚如何让VBA代码工作,所以它追逐推荐。
以下是我使用的不的VBA代码。它只打印“未找到”,即使使用PowerShell时相同的代码确实找到了一些东西。
Dim adConnection As ADODB.Connection
Dim adCommand As ADODB.Command
Dim adResults As ADODB.Recordset
Set adConnection = New ADODB.Connection
Set adCommand = New ADODB.Command
adConnection.ConnectionTimeout = 600
adConnection.Provider = "ADSDSOObject"
adConnection.Open "Active Directory Provider"
Set adCommand.ActiveConnection = adConnection
adCommand.Properties("Page Size") = 1000
adCommand.Properties("Size Limit") = 0
adCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
adCommand.Properties("Timeout") = 600
adCommand.Properties("Cache Results") = False
adCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS
adCommand.CommandTimeout = 600
adCommand.CommandText = "<GC://dc=company2,dc=com>;(&(objectClass=user)(samAccountName=user1));samAccountName;Subtree"
Set adResults = adCommand.Execute
If Not adResults Is Nothing Then
If Not adResults.EOF Then
Debug.Print adResults.RecordCount
Else
Debug.Print "not found"
End If
adResults.Close
End If
以下是执行的PowerShell代码:
[System.DirectoryServices.DirectoryEntry] $objDERoot = New-Object System.DirectoryServices.DirectoryEntry("GC://dc=company2,dc=com")
[System.DirectoryServices.DirectorySearcher] $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDERoot)
$objSearcher.SearchScope = "Subtree"
$objSearcher.ReferralChasing = "All"
$objSearcher.Filter = "(&(objectClass=user)(samAccountName=user1))"
$objSearcher.PropertiesToLoad.Add("samAccountName")
[System.DirectoryServices.SearchResultCollection] $colResults = $objSearcher.FindAll()
Foreach ($objResult in $colResults)
{
$objResult.Properties.Item("canonicalName")
}