#44482 - 2003-08-25 06:38 PM
Sorting problem due to LDAP format
|
JohnQ
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.
|
Top
|
|
|
|
#44483 - 2003-08-25 06:51 PM
Re: Sorting problem due to LDAP format
|
Shawn
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
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
|
Top
|
|
|
|
#44486 - 2003-08-25 08:00 PM
Re: Sorting problem due to LDAP format
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
$arrOUs=LDAPArray($Compldap, 'OU') for each $OU in $arrOUs ? $ou next
|
Top
|
|
|
|
#44489 - 2003-08-25 08:22 PM
Re: Sorting problem due to LDAP format
|
Radimus
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
|
Top
|
|
|
|
#44490 - 2003-08-25 08:24 PM
Re: Sorting problem due to LDAP format
|
maciep
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
|
|
|
|
#44492 - 2003-08-25 08:43 PM
Re: Sorting problem due to LDAP format
|
maciep
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
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
lookie... I'm Golfing with myself 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 [ 25. August 2003, 21:01: Message edited by: Radimus ]
|
Top
|
|
|
|
#44496 - 2003-08-26 12:58 AM
Re: Sorting problem due to LDAP format
|
Howard Bullock
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 ]
|
Top
|
|
|
|
#44497 - 2003-08-26 05:47 AM
Re: Sorting problem due to LDAP format
|
JohnQ
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
|
|
|
|
#44499 - 2003-08-26 11:54 AM
Re: Sorting problem due to LDAP format
|
JohnQ
Starting to like KiXtart
Registered: 2003-03-04
Posts: 171
|
Thanks for the encouragement Lonkero.
|
Top
|
|
|
|
#44500 - 2003-08-26 02:07 PM
Re: Sorting problem due to LDAP format
|
maciep
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 128 anonymous users online.
|
|
|