获取详细AD基于用户名

获取详细AD基于用户名

本文介绍了获取详细AD基于用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code检索用户从AD的详细信息,如电子邮件地址,电话号码等,等,codeS我目前使用的是:

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:

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.

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

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

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

我应该如何去检索的基础上提供的用户名从AD的细节?

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

推荐答案

LDAP的URI需要一个专有名称。账户名称将无法正常工作。如果您要根据你需要一个正规的LDAP查询帐户名称来获得用户对象:

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:

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

因为我不必编写所有的样板code一遍又一遍恼火,我在一个类包裹它(的前一段时间)。

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.

这篇关于获取详细AD基于用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:20