[英]Excel VBA - Getting a users LDAP string correct
I don't know what's wrong with me, but I can't get this string right! 我不知道我怎么了,但我无法正确输入此字符串! I've got this Excel sheet of user information and I want to connect to AD via LDAP, but I get this automation error '-2147217900 (80040e14)', which probably means there's a syntax error in the LDAP string.
我已经获得了此Excel用户信息表,并且希望通过LDAP连接到AD,但是却收到此自动化错误“ -2147217900(80040e14)”,这可能意味着LDAP字符串中存在语法错误。 Now, I use this function to pick up the users distinguished name.
现在,我使用此功能来选择用户专有名称。 Then I return that and try to pass it through adoConnection.Execute.
然后,我将其返回并尝试将其通过adoConnection.Execute传递。
The returned LDAP string looks like this: 返回的LDAP字符串如下所示:
<LDAP://CN=Bowie\,David,OU=Geniouses,OU=Music,DC=MasterDomain,DC=local>;ADsPath;subtree
The code looks like this: 代码如下:
ldapStr = "<LDAP://" & getUsersDN("dbowie") & ">;ADsPath;subtree"
Function like this: 像这样的功能:
Public Function getUsersDN(ByVal strUsername As String)
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=MasterDomain,dc=local' " & _
"WHERE objectCategory='user' " & _
"AND sAMAccountName='" & strUsername & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
getUsersDN = strDN
objRecordSet.MoveNext
Loop
End Function
try to wrap critical code to handle error, eg: 尝试包装关键代码以处理错误,例如:
on error resume next
Set objRecordSet = objCommand.Execute
if err.Number <> 0 then MsgBox "Exception occured: " & err.Description
on error goto 0
ok, try somthing other. 好的,尝试其他东西。 long ago i wrote stored procedure for that, may be it would help you
很久以前我为此写了存储过程,也许会对您有帮助
CREATE PROCEDURE sp_get_ad_user_info (
@DomainName varchar (64),
@AccountName varchar (128)
)
AS
BEGIN
DECLARE @adsiSQL nvarchar(1024)
SELECT @adsiSQL =
'SELECT samAccountName, Name, mail, Company, l [City], extensionAttribute1 [BirthDay], extensionAttribute2 [HireDay],department,title,telephoneNumber
FROM OPENQUERY( ADSI,
''SELECT samAccountName, Name, mail, company, l, extensionAttribute1, extensionAttribute2,department,title,telephoneNumber
FROM ''''LDAP://' + @DomainName + '''''
WHERE objectCategory = ''''Person'''' AND objectClass = ''''user'''' AND samAccountName=''''' + @AccountName + '''''' +
''')'
exec sp_executesql @adsiSQL
RETURN
END
I actually got the answer myself using AzAD Scriptomatic :) 我实际上是使用AzAD Scriptomatic自己得到答案的:)
Code now looks like this: 现在的代码如下所示:
Set objRootDSE = GetObject("LDAP://rootDSE")
Dim strQuery As String
strQuery = ("LDAP://" & getUsersDN("dbowie"))
Set objItem = GetObject(strQuery)
'***********************************************
'* End connect to an object *
'***********************************************
objItem.Put "description", "test"
objItem.SetInfo
Public Function getUsersDN(ByVal strUsername As String)
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=myDomain,dc=local' " & _
"WHERE objectCategory='user' " & _
"AND sAMAccountName='" & strUsername & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
getUsersDN = strDN
objRecordSet.MoveNext
Loop
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.