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 = fspenTextFile(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
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 = fspenTextFile(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