vba 根据用户名获取 AD 详细信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21110232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:33:45  来源:igfitidea点击:

Getting AD Details based on username

vbavbscriptactive-directory

提问by Bloopie Bloops

I have a code to retrieve the details of a user from the AD such as email address, phone number etc, etc. The codes I am currently using is:

我有一个代码可以从 AD 中检索用户的详细信息,例如电子邮件地址、电话号码等。我目前使用的代码是:

Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
msgbox(strUser)
Set objUser = GetObject("LDAP://" & strUser)

It gets the currently logged in user's details. But what I need to do now is to parse in the user's username and retrieve the details based on that.

它获取当前登录用户的详细信息。但是我现在需要做的是解析用户的用户名并基于此检索详细信息。

I have tried to change objSysinfo.UserName to the username and it returned blank.

我试图将 objSysinfo.UserName 更改为用户名,但它返回空白。

Set objSysInfo = CreateObject("ADSystemInfo")
strUser = "SomeUserName"
msgbox(strUser)
Set objUser = GetObject("LDAP://" & strUser)

How should I go about retrieving the details from the AD based on a user name provided?

我应该如何根据提供的用户名从 AD 中检索详细信息?

回答by Ansgar Wiechers

LDAP URIs require a distinguished name. Account names won't work. If you want to get user objects based on the account name you need a "regular" LDAP query:

LDAP URI 需要一个专有名称。帐户名称不起作用。如果要根据帐户名获取用户对象,则需要“常规”LDAP 查询:

username = "SomeUserName"

Set rootDSE = GetObject("LDAP://RootDSE")
base  = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
'filter on user objects with the given account name
fltr  = "(&(objectClass=user)(objectCategory=Person)" & _
        "(sAMAccountName=" & username & "))"
'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("distinguishedName").Value
  rs.MoveNext
Loop
rs.Close

conn.Close

Since I got annoyed from having to write all that boilerplate code over and over again, I wrapped it in a class (ADQuery) some time ago.

因为我对一遍又一遍地编写所有样板代码感到恼火,所以ADQuery前段时间我将它封装在一个类 ( ) 中。

回答by Michael Berg

Just an extra comment to Ansgar the RootDSE is great if you only have one domain. You can modify his code to point else where:

如果您只有一个域,那么对 Ansgar 的额外评论 RootDSE 是很棒的。您可以修改他的代码以指向其他位置:

    base  = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"

to something like:

类似于:

    base  = "<LDAP://" & "DC=corp,DC=foo,DC=com" & ">"

if your domain AD domain is corp.foo.com

如果您的域 AD 域是 corp.foo.com