Avatar of annayeg
annayeg asked on

ADfind to list a distribution lists for 20 users

Hi, I have been tasked to get a liist of distribution lists that about 20-25 users belong to.  Is there a way I can list my users in a textfile and pass that to adfind.

I have the following syntax but don't know how to do it for multiple users.
adfind -f "(&(objectCategory=user)(cn=lastname, firstname))" attr memberof >>c:\filename.csv

Active DirectoryPowershell

Avatar of undefined
Last Comment
rwskas

8/22/2022 - Mon
KenMcF

This might be easier with Powershell and Quest AD cmdlets if you do not have 2008R2

Or you could use adfind in a batch file with a for loop.

Put all the users into a file by username

$users = gc c:\users.txt
Foreach($user in $users){
get-qadmemberof $user | select name | out-file c:\$user.txt
}


This will export each users groups to their own file
ASKER
annayeg

I copied the commands to a text file and renamed it to script.ps1.  However, when I run it, I get an error message that the term get-quadmemberof is not recognized as the name of a cmdlet. function, script file or operable  program.

I am new with powershell, any help would be appreciated.

thanks
ASKER CERTIFIED SOLUTION
rwskas

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
KenMcF

You will need to install the Quest AD cmdlets.

http://www.quest.com/powershell/activeroles-server.aspx

Then add this line to the top of the PS1 file

add-pssnapin *Quest*
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
annayeg

When I ran the script, myoutputfile.txt  lists only the username's that I provided in the mytextfile.txt.  It doesn't list any of the dl's the users are in.

Any idea what I am doing wrong.

I changed my $targetOU = "LDAP://OU=offices,Dc=dc,dc=name,dc=net
rwskas

Looks like you have many dc's in their, try removing the dc=dc.
ASKER
annayeg

The name of our domain is dc.name.net
don't I need to specify it.    ou=offices, dc=dc,dc=name,dc=net
Here's what I run, with the exception of changing dc=name to our real name.

$users = Get-Content mytextfile.txt
$OutFile = "C:\hm\MyOutput.txt"

$TargetOU = "LDAP://OU=offices,DC=dc,DC=name,dc=net"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.SearchScope = "Subtree"
$objSearcher.PageSize = 1000

Foreach ($user in $Users) {
      Add-Content -Path $OutFile "$User"
      $objSearcher.Filter = "(&(objectCategory=User)(sAMAccountName=$User))"
      $colResults = $objSearcher.Findone()
            Foreach ($Group in ($ColResults.Properties.memberof)) {
            Add-Content -Path $OutFile "$Group"
            }
Add-Content -Path $OutFile ""
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
annayeg

I changed the (SamAccountName=$User) to (CN=$user) and it started working.  
Is it possible to surpress the dn information?  
rwskas

You bet, I added line 16, and changed line 17

$users = Get-Content mytextfile.txt
$OutFile = "C:\MyOutput.txt"

$TargetOU = "LDAP://OU=myou,DC=mysite,DC=com"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.SearchScope = "Subtree" 
$objSearcher.PageSize = 1000

Foreach ($user in $Users) {
	Add-Content -Path $OutFile "$User"
	$objSearcher.Filter = "(&(objectCategory=User)(cn=$User))"
	$colResults = $objSearcher.Findone()
		Foreach ($Group in ($ColResults.Properties.memberof)) {
			$GroupName = ([ADSI]"LDAP://$Group").Name
			Add-Content -Path $OutFile "$GroupName"
		}
Add-Content -Path $OutFile ""
}

Open in new window