Active Directory/Search for a user: Difference between revisions
(showed a simple ldap search example in Tcl) |
m (→{{header|Tcl}}: formatting) |
||
Line 30: | Line 30: | ||
This is just the basic setup. |
This is just the basic setup. |
||
<lang tcl>set Username "TestUser" |
|||
set Filter "((&objectClass=*)(sAMAccountName=$Username))" |
|||
set Base "dc=skycityauckland,dc=sceg,dc=com" |
|||
set Attrs distinguishedName</lang> |
|||
Now do the actual search. |
Now do the actual search. |
||
<lang tcl>set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</lang> |
|||
If we have only a single result its easy: |
If we have only a single result its easy: |
||
<lang tcl>if {[llength $result] == 1} { |
|||
puts [dict get [lindex $result 0 1] distinguishedName] |
puts [dict get [lindex $result 0 1] distinguishedName] |
||
}</lang> |
|||
} |
|||
Looping over the result set to output some values. |
Looping over the result set to output some values. |
||
<lang tcl>foreach pair $result { |
|||
lassign $pair cn attributes |
lassign $pair cn attributes |
||
puts [dict get $attributes distinguishedName] |
puts [dict get $attributes distinguishedName] |
||
} </lang> |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
puts [$e get distinguishedName] |
|||
}</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
[[Category:Active Directory]] |
[[Category:Active Directory]] |
Revision as of 20:36, 23 May 2009
You are encouraged to solve this task according to the task description, using any language you may know.
VBScript
Make sure you Connect to Active Directory
The search string and execution of the string
strUsername = "TestUser" strQuery = "<LDAP://dc=skycityauckland,dc=sceg,dc=com>;"_ & "(&(objectclass=*)(samaccountname=" & strUsername & "));distinguishedname;subtree" objCmd.ActiveConnection = objConn objCmd.Properties("Page Size")=100 objCmd.CommandText = strQuery Set objRS = objCmd.Execute
Doing something with a single result (this will output the returned users full DN)
If objRS.RecordCount = 1 Then WScript.Echo objRS.Fields("DistinguishedName") End If
Doing something with multiple results (this will output each returned users full DN)
If objRS.RecordCount > 0 Then For Each objUser in ObjRS WScript.Echo objRS.Fields("DistinguishedName") Next End If
Tcl
One can do it with the low level Connect to Active Directory based handle with this code:
This is just the basic setup. <lang tcl>set Username "TestUser" set Filter "((&objectClass=*)(sAMAccountName=$Username))" set Base "dc=skycityauckland,dc=sceg,dc=com" set Attrs distinguishedName</lang>
Now do the actual search. <lang tcl>set result [ldap::search $conn $Base $Filter $Attrs -scope subtree]</lang>
If we have only a single result its easy: <lang tcl>if {[llength $result] == 1} {
puts [dict get [lindex $result 0 1] distinguishedName]
}</lang>
Looping over the result set to output some values. <lang tcl>foreach pair $result {
lassign $pair cn attributes puts [dict get $attributes distinguishedName]
} </lang>
If you're bored you can also use this instead: <lang tcl>package require ldapx set conn [ldapx::connect $BindDN $Password] $conn traverse $Base $Filter $Attrs e {
puts [$e get distinguishedName]
}</lang>