Excel - 根据 Excel 工作表中的显示名称从 AD 获取用户名

发布于 2024-11-08 04:36:10 字数 150 浏览 0 评论 0原文

我有一张员工的 Excel 工作表,其中包含有关他们的所有信息。我正在尝试将此信息导入 AD,但为了做到这一点,我还需要获取这些用户的用户名。我想要 vba 代码或 vb.net 代码基本上获取 D 列中每一行的值并在 AD 中查找它并返回用户名并将其添加到 A 列。这样的事情可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-11-15 04:36:10

ADO.NET 可以实现这一点。我无法在当前环境中尝试它,但我过去已经成功使用过它:

添加对 ADO 类型库 (msadoXX.dll) 的引用。

使用您自己的凭据创建与 ActiveDirectory 的连接。有时,它无需指定您的凭据(单点登录)即可工作:

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

接下来创建一个命令:

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"

然后执行查询命令:

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

当然,您必须根据您的需要调整查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文