Link to home
Start Free TrialLog in
Avatar of Mination
Mination

asked on

How could I get a value from the Active Directory(eg:name) using an ASP script

How could I get a value from the Active Directory(eg:name) using an ASP script ,this is in an Intranet environment.
Active Directory Windows 2000
Avatar of pillbug22
pillbug22

Depends on what you want...are you wanting to retrieve a value based on a user?



Here's a VB script to pull all info about a given user name...it could be modified to be used on an ASP page instead of WSH:





' AD_query.vbs
' Selects AD info for all users (specified by criteria)
' Can be changed to select one user, members of specific OU, or
' all members in AD.


Option Explicit


Function ParseOU(strDN)
      Dim DNarray
      DNarray = Split(strDN, "=", -1, 1)
      ParseOU = Left(DNarray(3), 5)
End Function


Function ParseSubOU(strDN)
      Dim DNarray
      DNarray = Split(strDN, "=", -1, 1)
      ParseSubOU = Left(DNarray(2), 6)
End Function


Function ParseFirstName(strCN)
      Dim nameArray
      nameArray = Split(strCN, " ", -1, 1)
      ParseFirstName = nameArray(1)
End Function



Function ParseLastName(strCN)
      Dim nameArray
      nameArray = Split(strCN, " ", -1, 1)
      ParseLastName = nameArray(0)
End Function



Function GetDN(ADUserName)

      Dim ADProfileConn, ADProfileCmd, rsADProfile

      Set ADProfileConn = CreateObject("ADODB.Connection")
      ADProfileConn.Provider = "ADSDSOObject"
      ADProfileConn.Open "Active Directory Provider"
      Set ADProfileCmd = CreateObject("ADODB.Command")
      Set ADProfileCmd.ActiveConnection = ADProfileConn

                ' Change the following line to match your AD environment
      ADProfileCmd.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=adserver,dc=company,dc=com' WHERE objectClass='user' AND sAMAccountName='" & LCASE(ADUserName) & "'"
      Set rsADProfile = ADProfileCmd.Execute
      
      Do While Not rsADProfile.eof
            GetDN = "LDAP://" & rsADProfile.fields("distinguishedName")
            rsADProfile.MoveNext
      Loop
      
      Set rsAdProfile = Nothing
      Set ADprofileCmd = Nothing
      ADProfileConn.Close
      Set ADProfileConn = Nothing
      
End Function



Sub queryAD(ADUserName, OutputPath)
      Dim oObject, sProp, oClass, sAdsPath
      Dim fso, myFile, a, sLine, nums

      On Error Resume Next      

            Set fso = createObject("Scripting.FileSystemObject")
      Set myFile = fso.CreateTextFile(OutputPath, true)


      ' Bind to Active Directory object.
      Set oObject = GetObject(GetDN(ADUserName))

      Set oClass = GetObject(oObject.Schema)

      ' Enumerate mandatory properties of the object.
      For Each sProp In oClass.MandatoryProperties
            sLine = "(M) " & sProp & ": "
            If IsNull(oObject.GetEx(sProp)) Then
                  sLine = sLine & ""
            Else
                  nums = oObject.GetEx(sProp)
                  For Each a In nums
                        sLine = sLine & a & " "
                  Next
            End If
            myFile.WriteLine sLine
      Next

      ' Enumerate optional properties of the object.
      For Each sProp In oClass.OptionalProperties
            sLine = "(O) " & sProp & ": "
            If IsNull(oObject.GetEx(sProp)) Then
                  sLine = sLine & ""
            Else
                  nums = oObject.GetEx(sProp)
                  For Each a In nums
                        sLine = sLine & a & " "
                  Next
            End If
            myFile.WriteLine sLine
      Next
      
End Sub

'wscript.echo("Running...")

dim saveFile, UserName

saveFile = "c:\ldap.txt"

UserName = InputBox ("UserName to display info:", "Enter UserName")
If UserName = "" Then
    MsgBox("Must enter a user name!")
    WScript.quit(1)
End If

queryAd UserName, saveFile

wscript.echo("File has ben saved to " & saveFile)

Avatar of Mination

ASKER

I want to be able to lookup certain values within AD via a Intranet page i.e Phone number, Mobile...
You can use the above code to do pretty much what you want.

Just change the one line in there (commented out) to match your AD server name, then pass the username you are wanting to look up info for.

Or, you could try the following, which uses more of a SQL-type query with a returned recordset:





Function ParseOU(strDN)
      dim DNarray
      DNarray = Split(strDN, "=", -1, 1)
      ParseOU = Left(DNarray(3), 5)
End Function



Function ParseSubOU(strDN)
      dim DNarray
      DNarray = Split(strDN, "=", -1, 1)
      ParseSubOU = Left(DNarray(2), 6)
End Function



Function ParseFirstName(strCN)
      dim nameArray
      nameArray = Split(strCN, " ", -1, 1)
      ParseFirstName = nameArray(1)
End Function



Function ParseLastName(strCN)
      dim nameArray
      nameArray = Split(strCN, " ", -1, 1)
      ParseLastName = nameArray(0)
End Function



Sub DisplayADProfileInfo(ADUserName)

      ' The section below is added in order to retrieve user's info from Active Directory.
      ' If the fields which are available on the profile are changed, the following query string
      ' for AD might also have to be changed.

      Set ADProfileConn = CreateObject("ADODB.Connection")
      ADProfileConn.Provider = "ADSDSOObject"
      ADProfileConn.Open "Active Directory Provider"


      Set ADProfileCmd = CreateObject("ADODB.Command")
      Set ADProfileCmd.ActiveConnection = ADProfileConn
 
                ' Change the following line to match your AD server's name/address
                strServerPath = "dc=ADServer,dc=company,dc=com"

      ADProfileCmd.CommandText = "SELECT cn, sAMAccountName, mail, givenName, sn, distinguishedName, company, c FROM 'LDAP://" & strServerPath & "' WHERE objectClass='user' AND sAMAccountName='" & LCASE(ADUserName) & "'"

      
      wscript.echo(ADProfileCmd.CommandText)            
      set rsADProfile = ADProfileCmd.Execute


      on error resume next
      

      Set TextStream = FSO.CreateTextFile("C:\ADList.txt")
      
      do while not rsADProfile.eof
            tempVar = ""
            tempVar = tempVar & "CN: " & rsADProfile.fields("cn") & vbNewLine
            tempVar = tempVar & "SAMAccountName: " & rsADProfile.fields("sAMAccountName") & vbNewLine
            tempVar = tempVar & "Email Address: " & rsADProfile.fields("mail") & vbNewLine
            tempVar = tempVar & "Firstname: " & rsADProfile.fields("givenName") & vbNewLine
            tempVar = tempVar & "Surname: " & rsADProfile.fields("sn") & vbNewLine
            tempVar = tempVar & "Company: " & rsADProfile.fields("company") & vbNewLine
            tempVar = tempVar & "Country: " & rsADProfile.fields("c") & vbNewLine
            tempVar = tempVar & "DN: " & rsADProfile.fields("distinguishedName") & vbNewLine
            tempVar = tempVar & "OU: " & ParseSubOU(rsADProfile.Fields("distinguishedName")) & vbNewLine
            tempVar = tempVar & "----------------------------" & vbNewLine
            Set ou = GetObject("LDAP://OU=" & ParseSubOU(rsADProfile.Fields("distinguishedName")) & ",OU=" & ParseOU(rsADProfile.Fields("distinguishedName")) & "," & strServerPath)
            tempVar = tempVar & "OU: " & ParseOU(rsADProfile.Fields("distinguishedName")) & vbNewLine
            tempVar = tempVar & "Sub OU: " & ParseSubOU(rsADProfile.Fields("distinguishedName")) & vbNewLine
            tempVar = tempVar & "CN: " & ou.cn & vbNewLine
            tempVar = tempVar & "Description: " & ou.description & vbNewLine
            tempVar = tempVar & "Display Name: " & ou.displayName & vbNewLine
            tempVar = tempVar & "----------------------------" & vbNewLine
            tempVar = tempVar & "Truncated Name (use?): " & Left(ou.description, Len(ou.description)-6) & vbNewLine
            tempVar = tempVar & "----------------------------" & vbNewLine

               TextStream.WriteLine(tempVar)
               wscript.echo(tempVar)

            rsADProfile.movenext
      Loop

      TextStream.Close

      wscript.echo("Done")
      
            
End Sub


DisplayADProfileInfo("<username>")
Hey,

Just to let you know, you should be VERY careful when writing this code from a security standpoint. Use a great amount of input validation and only let the user input what YOU want them to input. Tricky people can use whats known as LDAP injection to get any information on any object in Active Directory.

Good Luck Mate!
Thanks for the Info! Well I must be dumb, I have changed the `strServerPath = "dc=luzifer,dc=mination,dc=net"` to reflect my server but i just get nothing back.

I am just going to a prompt and typing in "DisplayADProfileInfo username"

As you guessed I have never done this before :(

How would i turn this into a webpage if i get it working?
ASKER CERTIFIED SOLUTION
Avatar of pillbug22
pillbug22

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial