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
Active Directory Windows 2000
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(ADUse rName)
' 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.Connec tion")
ADProfileConn.Provider = "ADSDSOObject"
ADProfileConn.Open "Active Directory Provider"
Set ADProfileCmd = CreateObject("ADODB.Comman d")
Set ADProfileCmd.ActiveConnect ion = 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. CommandTex t)
set rsADProfile = ADProfileCmd.Execute
on error resume next
Set TextStream = FSO.CreateTextFile("C:\ADL ist.txt")
do while not rsADProfile.eof
tempVar = ""
tempVar = tempVar & "CN: " & rsADProfile.fields("cn") & vbNewLine
tempVar = tempVar & "SAMAccountName: " & rsADProfile.fields("sAMAcc ountName") & vbNewLine
tempVar = tempVar & "Email Address: " & rsADProfile.fields("mail") & vbNewLine
tempVar = tempVar & "Firstname: " & rsADProfile.fields("givenN ame") & vbNewLine
tempVar = tempVar & "Surname: " & rsADProfile.fields("sn") & vbNewLine
tempVar = tempVar & "Company: " & rsADProfile.fields("compan y") & vbNewLine
tempVar = tempVar & "Country: " & rsADProfile.fields("c") & vbNewLine
tempVar = tempVar & "DN: " & rsADProfile.fields("distin guishedNam e") & vbNewLine
tempVar = tempVar & "OU: " & ParseSubOU(rsADProfile.Fie lds("disti nguishedNa me")) & vbNewLine
tempVar = tempVar & "------------------------- ---" & vbNewLine
Set ou = GetObject("LDAP://OU=" & ParseSubOU(rsADProfile.Fie lds("disti nguishedNa me")) & ",OU=" & ParseOU(rsADProfile.Fields ("distingu ishedName" )) & "," & strServerPath)
tempVar = tempVar & "OU: " & ParseOU(rsADProfile.Fields ("distingu ishedName" )) & vbNewLine
tempVar = tempVar & "Sub OU: " & ParseSubOU(rsADProfile.Fie lds("disti nguishedNa me")) & 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(tempV ar)
wscript.echo(tempVar)
rsADProfile.movenext
Loop
TextStream.Close
wscript.echo("Done")
End Sub
DisplayADProfileInfo("<use rname>")
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(ADUse
' 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.Connec
ADProfileConn.Provider = "ADSDSOObject"
ADProfileConn.Open "Active Directory Provider"
Set ADProfileCmd = CreateObject("ADODB.Comman
Set ADProfileCmd.ActiveConnect
' Change the following line to match your AD server's name/address
strServerPath = "dc=ADServer,dc=company,dc
ADProfileCmd.CommandText = "SELECT cn, sAMAccountName, mail, givenName, sn, distinguishedName, company, c FROM 'LDAP://" & strServerPath & "' WHERE objectClass='user' AND sAMAccountName='" & LCASE(ADUserName) & "'"
wscript.echo(ADProfileCmd.
set rsADProfile = ADProfileCmd.Execute
on error resume next
Set TextStream = FSO.CreateTextFile("C:\ADL
do while not rsADProfile.eof
tempVar = ""
tempVar = tempVar & "CN: " & rsADProfile.fields("cn") & vbNewLine
tempVar = tempVar & "SAMAccountName: " & rsADProfile.fields("sAMAcc
tempVar = tempVar & "Email Address: " & rsADProfile.fields("mail")
tempVar = tempVar & "Firstname: " & rsADProfile.fields("givenN
tempVar = tempVar & "Surname: " & rsADProfile.fields("sn") & vbNewLine
tempVar = tempVar & "Company: " & rsADProfile.fields("compan
tempVar = tempVar & "Country: " & rsADProfile.fields("c") & vbNewLine
tempVar = tempVar & "DN: " & rsADProfile.fields("distin
tempVar = tempVar & "OU: " & ParseSubOU(rsADProfile.Fie
tempVar = tempVar & "-------------------------
Set ou = GetObject("LDAP://OU=" & ParseSubOU(rsADProfile.Fie
tempVar = tempVar & "OU: " & ParseOU(rsADProfile.Fields
tempVar = tempVar & "Sub OU: " & ParseSubOU(rsADProfile.Fie
tempVar = tempVar & "CN: " & ou.cn & vbNewLine
tempVar = tempVar & "Description: " & ou.description & vbNewLine
tempVar = tempVar & "Display Name: " & ou.displayName & vbNewLine
tempVar = tempVar & "-------------------------
tempVar = tempVar & "Truncated Name (use?): " & Left(ou.description, Len(ou.description)-6) & vbNewLine
tempVar = tempVar & "-------------------------
TextStream.WriteLine(tempV
wscript.echo(tempVar)
rsADProfile.movenext
Loop
TextStream.Close
wscript.echo("Done")
End Sub
DisplayADProfileInfo("<use
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!
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!
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Connec
ADProfileConn.Provider = "ADSDSOObject"
ADProfileConn.Open "Active Directory Provider"
Set ADProfileCmd = CreateObject("ADODB.Comman
Set ADProfileCmd.ActiveConnect
' Change the following line to match your AD environment
ADProfileCmd.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=adserver,dc=com
Set rsADProfile = ADProfileCmd.Execute
Do While Not rsADProfile.eof
GetDN = "LDAP://" & rsADProfile.fields("distin
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.Fi
Set myFile = fso.CreateTextFile(OutputP
' 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
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
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)