VBscript从CN搜索sAMAccountName

时间:2012-06-11 20:11:43

标签: asp-classic vbscript active-directory

我编写了这个脚本,它通过VBscript从AD中提取指定用户的sAMAccountName,但它似乎只能在我自己的OU组中工作。这是由于我公司内的权限限制吗?或者这是由于我在代码中没有看到的东西?

 Dim result
result = getsAMAccountName("Some Name")
msgbox result
Function getsAMAccountName(name)
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strsAM, objUser

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
msgbox strDNSDomain
strBase = "<LDAP://" & strDNSDomain & ">"

'be sure passed var usersel is referenced properly
strFilter = "(cn=" & name & ")"
strAttributes = "distinguishedName"

strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF

    strsAM = adoRecordset.Fields("distinguishedName").Value
    Set objUser = GetObject("LDAP://" & strsAM)
    getsAMAccountName = objUser.sAMAccountName
    adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close
End Function

2 个答案:

答案 0 :(得分:0)

在GetObject调用中指定OU时是否有效?

GetObject("LDAP://OU=YourOU,DC=YourDomain,DC=com")

从这个问题Querying Active Directory using VBScript

答案 1 :(得分:0)

结束了权限,确保在将LDAP拉到asp经典时包含/指定processID和PW ...并避免使用asp经典

Set adoConnection = CreateObject("ADODB.Connection")                
            adoConnection.Provider = "ADsDSOObject"
                With adoConnection
                    .Properties("User ID") = ' Process ID goes
                    .Properties("Password") = 'password
                    .Properties("encrypt password") = True
                End With

        adoConnection.Open "Active Directory Provider"
        Set adoCommand = CreateObject("ADODB.Command")
        Set adoCommand.ActiveConnection = adoConnection