开发者

Excel - Get username from AD based on display name in Excel sheet

So I've got this Excel sheet of employees which have all the information about them. I'm trying to import this info into AD, but in order开发者_开发问答 to do that I need to get the username for these users as well. And I would like vba code or vb.net code that basically takes the value of every row in column D and looks for it in AD and returns the username and adds it to column A. Would something like this be possible?


It is possible with ADO.NET. I can't try it in my current environment but I've used it successfully in the past:

Add a reference to the ADO type library (msadoXX.dll).

Create a connection to ActiveDirectory using your own credentials. Sometimes it works without specifying your credentials (single sign on):

Dim Con As New Connection
con.Provider = "ADsDSOObject"
oConnect.Properties("User ID") = "userme"
oConnect.Properties("Password") = "xxxx"
oConnect.Properties("Encrypt Password") = True
oConnect.Open "MyExcelConnection", stUser, stPass

Next create a command:

Dim command As New Command
Set command.ActiveConnection = oConnect
command.CommandText =
    "<LDAP://dc.company.com/ou=accounting,dc=company,dc=com>;(objectClass=user);displayName,mail;subtree"

Then execute the query command:

Dim rs As ResultSet
Set rs = cmd.Execute
While Not rs.EOF
    X = rs.Fields(0).Value
    Y = rs.Fields(1).Value
    rs.MoveNext
Wend

Of course, you'll have to adapt the query to your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜