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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AD Groups Return to Only single Line

Status
Not open for further replies.

mholz24

IS-IT--Management
Mar 5, 2016
2
AU
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
Next
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)
f.write(GroupsList)
Dim arrPath = Split(GroupsList, "")
f.Close

Else
MessageBox.Show("Couldn't find user " & StrUserName & " in AD.")
End If
ObjRS.Close : ObjRS = Nothing
ObjConn.Close : ObjConn = Nothing
 
Well, if I'm understanding you correctly, removing the vbCrLf (which adds the new line) would do what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top