Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AD Groups Return to Only single Line

AD Groups Return to Only single Line

AD Groups Return to Only single Line

Hi all, I am Very new to scripting
I am currently been looking for a way to call for an inputted user then export to txt file with AD group members
it's working but I am unable to work out how to stop it from returning a new line for every single user group.
here is the code I'm working with, maybe you have a better idea?

Const ForAppending = 8
Const CreateIfNotExist = True
Const OpenAsASCII = 0

Dim ObjUser, ObjRootDSE, ObjConn, ObjRS
Dim GroupCollection, ObjGroup
Dim StrUserName, StrDomName, StrSQL
Dim GroupsList
Dim WriteFile
Dim strShare = "\\myserver\ShareFolder"
Dim strLogFile = "record.log"
GroupsList = ""

ObjRootDSE = GetObject("LDAP://RootDSE")
StrDomName = Trim(ObjRootDSE.Get("DefaultNamingContext"))
ObjRootDSE = Nothing

StrUserName = UsernameInput.Text
StrSQL = "Select ADsPath From 'LDAP://" & StrDomName & "' Where ObjectCategory = 'User' AND SAMAccountName = '" & StrUserName & "'"

ObjConn = CreateObject("ADODB.Connection")
ObjConn.Provider = "ADsDSOObject" : ObjConn.Open("Active Directory Provider")
ObjRS = CreateObject("ADODB.Recordset")
ObjRS.Open(StrSQL, ObjConn)
If Not ObjRS.EOF Then
ObjRS.MoveLast : ObjRS.MoveFirst
ObjUser = GetObject(Trim(ObjRS.Fields("ADsPath").Value))
GroupCollection = ObjUser.Groups
'Groups with direct membership, and calling recursive function for nested groups
For Each ObjGroup In GroupCollection
GroupsList = GroupsList + ObjGroup.CN + vbCrLf
ObjGroup = Nothing : GroupCollection = Nothing : ObjUser = Nothing
'Writing list in a file named Groups <username>.txt
WriteFile = CreateObject("WScript.Shell")
Dim fso, f
fso = CreateObject("Scripting.FileSystemObject")
f = fso.OpenTextFile(strShare & "\" & strLogFile, ForAppending, CreateIfNotExist, OpenAsASCII)
Dim arrPath = Split(GroupsList, "")

MessageBox.Show("Couldn't find user " & StrUserName & " in AD.")
End If
ObjRS.Close : ObjRS = Nothing
ObjConn.Close : ObjConn = Nothing

RE: AD Groups Return to Only single Line

Well, if I'm understanding you correctly, removing the vbCrLf (which adds the new line) would do what you want.

RE: AD Groups Return to Only single Line

thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close