Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trimming the DistinguishedName and MemberOf info.

Status
Not open for further replies.

cmwoodman

IS-IT--Management
Sep 20, 2000
32
0
0
US
I have a script that will drill down a specific OU, to sub OU's and then list all of the users and the groups that user is a member of. It works well but i have a problem, i cannot format the results correctly.

What i get back looks like this:

CN=Testing3,OU=NA,OU=Portal,DC=TestDomain,DC=com

, , ,CN=GPI,OU=Portal,DC=TestDomain,DC=com
, , ,CN=Primary Global,OU=Portal,DC=TestDomain,DC=com
, , ,CN=Ariba,OU=Portal,DC=TestDomain,DC=com


(I placed the ", , ," just so i could find the break in the information.)

what i would like to have is this:

NA, Testing3, GPI
NA, Testing3, Primary Global
NA, Testing3, Ariba


I just can't figure out how to trim the information. If anyone could point me in the right direction i would greatly appreciate it. Thanks.

Below is the code that returns the results above:

PS - (I don't remember where i borrowed the code from so if it is from someone here thanks.)


Option Explicit

On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D

Dim objDSE, strDefaultDN, strDN, objContainer

'Create the Text file
Dim fs, res
Set fs = WScript.CreateObject("Scripting.FileSystemObject")
Set res = fs.CreateTextFile("c:\Users.txt", True)
res.Writeline ("Users details")

'Select the current Domain
Set objDSE = GetObject("LDAP://rootDSE")
strDefaultDN = objDSE.Get("defaultNamingContext")

strDN = strDefaultDN

'This is just a loop that will return all of the users and OU's
Set objContainer = GetObject("LDAP://" & strDN)
Call ListUsers(objContainer)

Sub ListUsers(objADObject)
Dim objChild, arrMemberOf, item
For Each objChild in objADObject
Select Case objChild.Class
Case "user"
res.Writeline objChild.Get("DistinguishedName")
arrMemberOf = objChild.GetEx("memberOf")
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
res.Writeline "The memberOf attribute is not set."
Else
res.Writeline " "
For each item in arrMemberOf
res.Writeline ", , ," & Item
Next
End If
Case "organizationalUnit" , "container"
Call ListUsers(objChild)
End select
Next
End Sub
 
I would check for length and then use

Left(string, length)


to get the truncated version on what you need

so:

Code:
if len(string)>someValue
   shortStr=Left(string, 8)
end if

hth

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Bastien,

I am not sure what you mean, checking for length does not seem to really help me since just about everything is going to be a different length. I think i what i need to do is some sort of Replace(string, "what to replace", "with this") command but the problem i am having with that is i can only replace one at a time. and at the 'for each item' line it does not work real well there. and other ideas?
 
Sorry misintrepreted the "trim" requirement...I thik the best way is to place the line of data into an array and then use the split function to break the array up...the move thru the array and isolate the items you need

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Hello cmwoodman,

Try this, but it depends very much on the case of "CN" & "OU" which may be not a problem.
Code:
Sub ListUsers(objADObject)
    Dim objChild, arrMemberOf, item, sDN_base, sDN_branch
    For Each objChild in objADObject
        Select Case objChild.Class
            Case "user"
                sDN = objChild.Get("DistinguishedName")
                sDN_base=Replace(split(sDN,",")(0),"CN=","") & "," & Replace(split(sDN,",")(1),"OU=","")
                res.Writelin sDN
                arrMemberOf = objChild.GetEx("memberOf")
                If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
                     res.Writeline "The memberOf attribute is not set."
                Else
                     res.Writeline " "
                     For each item in arrMemberOf
                         sDN_branch = Replace(split(item,",")(0),"CN=",""))
                         res.Writeline sDN_base & "," & sDN_branch
                     Next
                End If
            Case "organizationalUnit" , "container"
                Call ListUsers(objChild)
        End select
    Next
End Sub
regards - tsuji
 
Correction: To display OU before CN, switch the (0) and (1) in the sDN_base, ie, the line should be read:
Code:
sDN_base=Replace(split(sDN,",")(1),"OU=","") & "," & Replace(split(sDN,",")(0),"CN=","")
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top