【问题标题】:Find user by sAMAccountName VBA通过 sAMAccountName VBA 查找用户
【发布时间】:2015-01-28 15:38:09
【问题描述】:

我正在尝试通过 sAMAccountName 在 AD 中查找用户。这是我的代码:

sQuery = "<LDAP://OU=theOU,DC=mainDC,DC=com>;(&(objectClass=user)(objectCategory=Person)(sAMAccountName=sAMA));distinguishedName,sAMAccountName;subtree"

我执行这个查询

Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"
Set rs = conn.Execute(sQuery)

查询没有失败。我也尝试将查询更改为:

<LDAP://OU=theOU,DC=mainDC,DC=com>;(&(objectClass=user)(objectCategory=Person)(sAMAccountName=sAMA));subtree

但这也失败了。

我在 VBA 中执行此操作,非常感谢任何帮助!

我得到的错误是:

A referral was returned from the server.

另外,除了查询两次之外,有没有更简单的方法来搜索多个域?

谢谢!

【问题讨论】:

  • 它是什么 - 不返回任何东西?还是失败了?对于初学者,我想说objectclass=userobjectcategory=Person 是多余的。您的用户的 samaccountname 是 sAMA
  • 失败了。是的,用户的 samaaccountname 是 sAMA
  • 我相信它抛出的关于返回的引用的错误?
  • 我用错误更新了主帖。
  • 推荐不是错误,它是服务器响应说它无法在本地找到您要求的内容,但它提供了另一台服务器。推荐还返回了哪些其他信息?

标签: vba excel active-directory ldap ldap-query


【解决方案1】:

找到这个sn-p,试一试。将第 1 行更改为您的 AD,将第 2 行更改为 sAMA:

StartNode = "cn=Users,dc=fabrikam,dc=com" 'edit with your values
strAccount = "HMustermann" 'edit with your searchvalue

Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
SearchScope = "subtree"

FilterString = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & strAccount & "))"
Attributes = "adspath"

LDAPQuery = "<LDAP://" & StartNode & ">;" & FilterString & ";" _
        & Attributes & ";" & SearchScope

objCommand.CommandText = LDAPQuery
objCommand.Properties("Page Size") = 1500
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

Set objRecordset = objCommand.Execute

If Not objRecordset.EOF Then
   objRecordset.MoveFirst

   Do Until objRecordset.EOF
      strUserPath = objRecordset.Fields("ADsPath").Value
      Set objUser = GetObject(strUserPath)
      '-------get attributes -----------
      MsgBox objUser.DisplayName
      '--------------------------------------
      objRecordset.MoveNext
   Loop
End If

objRecordset.Close
objConnection.Close
MsgBox "Finish"

【讨论】:

  • 我也试过了,但仍然没有解决我的问题。不过,我确实设法弄清楚了,所以我将其添加为答案。不过还是谢谢! :)
【解决方案2】:

A referral was returned from the server. 通常表示您在连接到 domainB 时尝试获取 domainA 中的对象。

请尝试使用:

<LDAP://mainDC.com/OU=theOU,DC=mainDC,DC=com>

代替:

<LDAP://OU=theOU,DC=mainDC,DC=com>

如果没有服务器,它将连接到当前计算机(或用户?)的域,可能不是 mainDC.com。

要从林中的所有域中搜索,您可以使用 GC:

<GC://[GC server]>

但请注意,GC 上只存在一部分属性。

【讨论】:

    【解决方案3】:

    原来我所要做的就是添加这个:

    objCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS
    

    连接代码为:

    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open "Provider=ADsDSOObject;"
    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
    objCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS
    
    Set rootDSE = GetObject("LDAP://RootDSE")
    Set dom = GetObject("LDAP://" & rootDSE.Get("defaultNamingContext"))
    objCommand.CommandText = "<" & dom.ADsPath & ">;" & _
        "(&(objectClass=user)(objectCategory=Person)(sAMAccountName=" & LoginName & "));" & _
        "distinguishedName,sAMAccountName;subtree"
    Set objRecordSet = objCommand.Execute
    

    现在可以正常工作了。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2015-12-23
      • 2018-01-04
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多