Avatar of annayeg
annayeg asked on

script needed to query AD

Hi,
I have 25-30 users that I already have the distribution lists that they belong to.  I need to find out if any of the dls are exclusive to those users.  Any dl that only consists of these users.
Is there a way to get this done?
Thanks
Active DirectoryPowershell

Avatar of undefined
Last Comment
annayeg

8/22/2022 - Mon
KenMcF

You can do this with the quest AD cmdlets.
 
Try this, this will import the users from a text file if needed. I have not tested.

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

$users = gc c:\users.txt
Foreach ($user in $users){
$groups = get-qadmemberof $user
foreach ($group in $groups){
$count = (get-qadgroupmember $group).count
if ($count -eq 1){
Write-host "$(user.name) is the only member of $($group.name)"
}}}
KenMcF

Here is an example using the MS AD cmdlets

$users = gc c:\users.txt
foreach ($user in $users){
$groups = (get-aduser $user -properties memberof).memberof
foreach ($group in $groups){
$count = (get-adgroupmember $group).count
If ($count -eq 1){
Write-host "$(user.name) is the only member of $($group)"
}}}
ASKER
annayeg

I used the following powershell to get a listing of the dls users from mytextfile.txt belong to.  Can I modify this script to find out if any of the dls are exclusive to those users?  

$users = Get-Content mytextfile.txt
$OutFile = "C:\test\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)(displayname=$User))"
      $colResults = $objSearcher.Findone()
            Foreach ($Group in ($ColResults.Properties.memberof)) {
            Add-Content -Path $OutFile "$Group"
            }
Add-Content -Path $OutFile ""

}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
KenMcF

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
ASKER
annayeg

I tried the above script but it gave me an output with the names of the users not names of the groups.  Any ideas?
thanks
ASKER
annayeg

wasn't completely what I wanted.