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

Active Directory Groups for a list of Users

Status
Not open for further replies.

darkbug

IS-IT--Management
Feb 28, 2012
2
US
Hello, I have been lurking here for about 8 years.

I need some help with finding what AD groups a list of users are members of. I have a list of usernames for my domain in a text file.

What would be the easiest way to export that list of username's AD groups to a text file?

Any help will be greatly appreciated!

Take Care,
-DB
 
This is a little bit of code I have for displaying my AD groups. I'm sure with a bit of modifying, you'll be able to achieve what you want

Code:
On Error Resume Next
dim objSysInfo, strADUserPath

Set objSysInfo = CreateObject("ADSystemInfo")

strUserPath = "LDAP://" & objSysInfo.UserName

Set objUser = GetObject(strUserPath)

objUser.GetInfo

strGroups = objUser.Get("memberOf")

for each group in strGroups
   wscript.echo group
next
 
Found the following code at this website:


Code:
Option Explicit
Dim objNetwork, strDomain, strUser, objUser, objGroup, strGroupMemberships

' Get the domain and username from the WScript.Network object
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName

' Instanciate the user object from the data above
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser)

' Run through the users groups and put them in the string
For Each objGroup In objUser.Groups
    strGroupMemberships = strGroupMemberships & objGroup.Name & ","
Next

MsgBox strGroupMemberships

Open your text file, looping through each user as the strUser variable and instead of a msgbox, write some code to output to a file
 
Thank you for your help! Here is what I came up with:
Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

'Open list.txt file
file = "user_list.txt"
Set ts = fso.OpenTextFile(file, ForReading)
Set objNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Srcpath = objFSO.GetAbsolutePathName(".")   'Use curent path as destination

Set objTextFile = objFSO.OpenTextFile(Srcpath + "\User_Groups.XLS", ForAppending, True)


' Get the domain from the WScript.Network object
	strDomain = objNetwork.UserDomain
		
Do Until ts.AtEndOfStream
	struser = ts.ReadLine

' Instanciate the user object from the data above
		Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser)

' Run through the users groups and put them in the string
		For Each objGroup In objUser.Groups
			strGroupMemberships = strGroupMemberships & objGroup.Name
			objTextFile.WriteLine(struser & vbTab & strGroupMemberships)
			strGroupMemberships = Null
		Next


	loop
ts.close
objTextFile.Close
MsgBox "Done"

Thank you for your help,
DB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top