VBScript通过传递家庭电话查找Active Directory用户名

时间:2014-01-15 21:43:13

标签: vbscript active-directory wsh ldap-query

我对使用LDAP查询的Active Directory和WSH Scripting有点不熟悉。如果我将Home Telephone字段作为AD / LDAP查询的参数提供,我有奇怪的请求能够自动(仅通过VBScript)从AD服务器获取Active Directory用户名。

有谁知道这是否可行?

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了感兴趣的人的解决方案:

Set rootDSE = GetObject("LDAP://RootDSE")
base  = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
'filter on user objects with the given account name
fltr  = "(&(objectClass=user)(objectCategory=Person)" & _
        "(homePhone=yourphone))"
'add other attributes according to your requirements
attr  = "distinguishedName,sAMAccountName"
scope = "subtree"

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & fltr & ";" & attr & ";" & scope

Set rs = cmd.Execute
Do Until rs.EOF
  WScript.Echo rs.Fields("sAMAccountName").Value
  rs.MoveNext
Loop
rs.Close

conn.Close