Page 1 of 1 1
Topic Options
#44482 - 2003-08-25 06:38 PM Sorting problem due to LDAP format
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
This is a stupid one...

I have a database full of machine account names and their OUs. The OU field is in standard LDAP format. The probelm is if you try to sort anything by OU, the sort is not accurate because of the LDAP name (basically reversed). Does anyone know of a way to "flip" that around? The LDAP path was collected via script when I enumerated the OU and sub OUs for machine accounts.
[Confused]

Top
#44483 - 2003-08-25 06:51 PM Re: Sorting problem due to LDAP format
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Not a stupid question at all - tricky one actually. Suppose you could slice and dice something like this to generate a reverse DN, would think that jlo could SplitJoin something more eloquent though ...

break on

$=setoption("wrapateol","on")

$dn = "LDAP://OU=XXX,OU=YYY,OU=ZZZ,DC=AAA,DC=BBB,DC=CCC,DC=ca"

$ou = substr($dn,8,instr($dn,"DC=")-9)
$dc = substr($dn,instr($dn,"DC="))

$newdn = "LDAP://" + $dc + "," + $ou

? $newdn

-Shawn

[ 25. August 2003, 18:53: Message edited by: Shawn ]

Top
#44484 - 2003-08-25 06:56 PM Re: Sorting problem due to LDAP format
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
use it to return an array of all the good stuff from an LDap string... or just the specific type of element required (CN OU DC)

code:
 
Function LDAPArray($ldap, optional $filter)
dim $item, $element, $temp
if not instr("CN OU DC",$filter)
$filter=''
endif
$item=-1
$tarr = split($ldap,',')
for $element = 0 to ubound($tarr)
$temp=$tarr[$element]
if left($temp,2)=$filter or $filter=''
$item=$item+1
redim preserve $LDAParray[$item]
$LDAParray[$item]=right($temp,len($temp)-3)
endif
next
endfunction

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#44485 - 2003-08-25 07:41 PM Re: Sorting problem due to LDAP format
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Sorry Shawn, I should have been more specific. I'm not too concerned with the $dc string - it's pretty much a given.

What I really need to reverse is the OU string.

Taking your example OU=XXX,OU=YYY,OU=ZZZ. This is what I need to reverse.

The end result would be OU=ZZZ,OU=YYY,OU=XXX. (Just as if you were drilling down through AD users and computers.)

Top
#44486 - 2003-08-25 08:00 PM Re: Sorting problem due to LDAP format
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$arrOUs=LDAPArray($Compldap, 'OU')
for each $OU in $arrOUs
? $ou
next
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#44487 - 2003-08-25 08:11 PM Re: Sorting problem due to LDAP format
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Rad, is your function dropping the first OU ? think it might be, the first OU after the // ... then just enum backwards through the array and rebuild the string, kinda got it working but would be great to use the UDF me thinks ...
Top
#44488 - 2003-08-25 08:17 PM Re: Sorting problem due to LDAP format
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hmmm, guess there would always be a CN before the any OU or DC qualifier so never mind, my boo.
Top
#44489 - 2003-08-25 08:22 PM Re: Sorting problem due to LDAP format
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
code:
 
break on

$ldap = TranslateName(1, @domain, 3, @ldomain+'\'+@userid, 1)
? $ldap[0]

$OU = LDAPArray($ldap[0])
for each $element in $ou
? $element
next

$OU = LDAPArray($ldap[0],'OU')
for each $element in $ou
? $element
next

;*************************************************************************************************************************
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
Dim $NameTranslate, $ReturnName, $Error, $ErrorText
$Error = 0 $ErrorText = "" $ReturnName = ""
$NameTranslate = CREATEOBJECT ("NameTranslate") $Error = @error $ErrorText = @serror
if $Error = 0
$NameTranslate.Init ($InitType, $BindName) $Error = @error $ErrorText = @serror
if $Error = 0
$NameTranslate.Set ($LookupNameType, $LookupName) $Error = @error $ErrorText = @serror
if $Error = 0
$ReturnName = $NameTranslate.Get($ReturnNameType) $Error = @error $ErrorText = @serror
endif
endif
endif
$TranslateName = $ReturnName, $Error, $ErrorText
Endfunction
;*************************************************************************************************************************
Function LDAPArray($ldap, optional $filter)
dim $item, $element, $temp
if not instr("CN OU DC",$filter)
$filter=''
endif
$item=-1
$tarr = split($ldap,',')
for $element = 0 to ubound($tarr)
$temp=$tarr[$element]
if left($temp,2)=$filter or $filter=''
$item=$item+1
redim preserve $LDAParray[$item]
$LDAParray[$item]=right($temp,len($temp)-3)
endif
next
endfunction


_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#44490 - 2003-08-25 08:24 PM Re: Sorting problem due to LDAP format
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
What about something like this. Might be not be general enough, but you get the idea.

code:
$newDN = ""
$dn = "LDAP://OU=XXX,OU=YYY,OU=ZZZ,DC=AAA,DC=BBB,DC=CCC,DC=ca"
$justOUs = split(split(split($dn, "//")[1], ",DC")[0], ",")

for $j = ubound($justOUs) to 0 step -1
$newDN = $newDN + $justOUs[$j] + ","
next
$newDN = substr($newDN, 1, len($newDN)-1)

? $newDN

_________________________
Eric

Top
#44491 - 2003-08-25 08:30 PM Re: Sorting problem due to LDAP format
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
maciep

Could you mod to account for CN, example:

"LDAP://CN=SHAWN,OU=XXX,OU=YYY,OU=ZZZ,DC=AAA,DC=BBB,DC=CCC,DC=ca"

-Shawn

Top
#44492 - 2003-08-25 08:43 PM Re: Sorting problem due to LDAP format
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I'm assuming you mean to ignore them.

code:
$newDN = ""
$dn = "LDAP://CN=SHAWN,OU=XXX,OU=YYY,OU=ZZZ,DC=AAA,DC=BBB,DC=CCC,DC=ca"
$justOUs = split(split(split($dn, "//")[1], ",DC")[0], ",")

for $j = ubound($justOUs) to 0 step -1
if substr($justOUs[$j], 1, 2) <> "CN"
$newDN = $newDN + $justOUs[$j] + ","
endif
next
$newDN = substr($newDN, 1, len($newDN)-1)

? $newDN

But at this point I would just start doing what rad is doing. And asking for some sort of filter that you want to see (or don't want to see). And then I wouldn't split on ",DC" and instead just make it more general
_________________________
Eric

Top
#44493 - 2003-08-25 08:58 PM Re: Sorting problem due to LDAP format
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
lookie... I'm Golfing with myself [Big Grin]
code:
 
Function LDAPArray($l, optional $f)
dim $e, $t
if not instr("CN OU DC",$f) $f='' endif
for each $e in split($l,',')
if left($e,2)=$f or $f=''
$t=$t+'|'+right($e,len($e)-3)
endif
next
$LDAParray=split(substr($t,2),'|')
endfunction

to delonk it:
$e=$element
$t=$temp
$l=$ldap
$f=$filter
[Big Grin]

[ 25. August 2003, 21:01: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#44494 - 2003-08-25 08:59 PM Re: Sorting problem due to LDAP format
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ja, das is good. I think for John's purposes this routine would work, and Rad's UDF will work too if enumed backwards.
Top
#44495 - 2003-08-25 10:24 PM Re: Sorting problem due to LDAP format
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
You can also use this cool tool by Chris.

KiXforms - Active Directory Browser
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=13;t=000607

[ 25. August 2003, 22:28: Message edited by: NTDOC ]

Top
#44496 - 2003-08-26 12:58 AM Re: Sorting problem due to LDAP format
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
AARRRGGGHHHH! You guys work too hard.

Use TranslateName to get the correct data for easy sorting.

code:
$Conicalname = TranslateName (3, "", 3, @Domain + "\" + @wksta + "$$", 2)  

yields:

us.tycoelectronics.com/Organizations/NCS/9826/test/BULLOCKHA

split and sort.

[ 26. August 2003, 00:58: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#44497 - 2003-08-26 05:47 AM Re: Sorting problem due to LDAP format
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Howard, you always have a solution for everything.

I'm using a modified version of the UDF that you wrote a while back to enumerate OUs. View original code here

Is there any way to tweak this UDF to output the LDAP string in a "reversed" sortable fashion?

Top
#44498 - 2003-08-26 06:37 AM Re: Sorting problem due to LDAP format
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure. [Big Grin]
_________________________
!

download KiXnet

Top
#44499 - 2003-08-26 11:54 AM Re: Sorting problem due to LDAP format
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Thanks for the encouragement Lonkero. [Razz]
Top
#44500 - 2003-08-26 02:07 PM Re: Sorting problem due to LDAP format
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Here's the one I wrote to enum(count) computers in AD. This will print out the current OU in an indented style to console. But basically you have the root and can use the earlier posts to reverse it.

code:
Break ON
dim $total
$total = enumComputers("OU=blah, DC=blah, DC=blah, DC=net")

? "Final Count: " $total

function enumComputers($root, optional $indent)
dim $ouFilter, $compFilter[0]
dim $rootObj, $object, $comp
dim $fsPos, $count

$ouFilter = 'organizationalunit', 'container'
$compFilter[0] = 'Computer'
$count = 0

; Display current OU or Container
for $j = 0 to $indent
" "
next
split($root, ",")[0] ?

; If the OU or Container contains a forward slash (there are three of them)
; Add a backslash (esc char) in front of it
$fsPos = instr($root, '/')
if $fsPos
$root = substr($root, 1, $fsPos-1) + '\' + substr($root, $fsPos)
endif

$rootObj = getobject("LDAP://"+$root)
if @error = 0
; Call this function (I love recursion)
; with each child OU or Container
$rootObj.Filter = $ouFilter
for each $object in $rootObj
$count = $count + enumComputers($object.Name + ", " + $root, $indent+3)
next

; Count the number computers in the
; current OU or Container
$rootObj.Filter = $compFilter
for each $comp in $rootObj
$count = $count + 1
next
endif
$enumComputers = $count
endfunction

_________________________
Eric

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 128 anonymous users online.
Newest Members
SERoyalty, mytar, Gabriel, Alex_Evos, Dansen
17869 Registered Users

Generated in 0.086 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org