从VB脚本中获取Active Directory中的网络详细信息?

时间:2017-07-10 12:10:12

标签: vbscript asp-classic active-directory windows-authentication

我们需要从Active Directory填充用户详细信息。共享我的代码以从AD访问用户详细信息。

1 个答案:

答案 0 :(得分:0)

简介

人们经常要求通过传递网络帐户名或电子邮件来查询活动目录。有很多文章可以提供,有人可能会感到困惑。以下是从活动目录数据库访问用户详细信息的简单工作代码 - 请注意,要访问AD,您必须指定有效的网络帐户凭据。

function GetADDetails(userId)
  Set objConn = Server.CreateObject("ADODB.Connection")
  objConn.provider ="ADsDSOObject"
  objConn.Properties("User ID") = "domain\userId" 'specify domain and 
  network account
  objConn.Properties("Password") = "password" 'specify network password
  objConn.Properties("Encrypt Password") = True
  objConn.open "Active Directory Provider"
  Set objCom = CreateObject("ADODB.Command")
  Set objCom.ActiveConnection = objConn
  strTarget="GC://abc.com" 'your domain name
  objCom.CommandText ="select sn, givenName, sAMAccountName, name,mail,      
  telephoneNumber FROM '"+strTarget+"' where sAMAccountname='"+userId+"'"
  Set objRS = objCom.Execute
  If Not (objRS.EOF Or objRS.BOF) Then
    GetADDetails=objRS.GetRows
  Else
    GetUserData = Null
  End If
  'Close objects and remove from memory
  objRS.Close
  objConn.Close
  Set objRS = Nothing
  Set objConn = Nothing
  Set objCom = Nothing
end function