Determine if user can send to DL

Recently we've had a rash of SPAM email messages going to distribution lists. This has led to the limiting the permissions on who can send to these lists. For the largest lists, we are assigning mail-enabled security groups permissions to send to these lists.

Problem: Some of these mail-enabled groups are very large, containing nested lists inside of other lists. Trying to determine if a specific user has permissions to send to a list can be difficult.

That's the purpose of this script. It recurses each group until returning a raw list of all the mailboxes allowed to send to the specific distribution list. Using a -match, it can quickly tell you if a specific person is allowed to send to that list.

function Get-DLMembers ([string]$item, [array]$done) {
#Recurse distribution list, to get all the members
        $x += 1
        $members = @()
        if ((($done -match $item).count -eq 0) -or ($done.count -eq 0)) {
                $done += $item
                $mems = get-distributiongroupmember $item | sort recipienttype, displayname
                foreach ($m in $mems) {
                        $members += $m
                        if ($m.recipienttype -eq "UserMailbox" -or $m.recipienttype -eq "mailcontact") {
                                Write-Host ("-"*$x) $m.displayname
                        } elseif ($item -eq $m.displayname) {
                                Write-Host "loop:" $item " is a member of " $m.displayname
                        } else {
                                Write-Host ("-"*$x) $m.displayname                             
                                get-dlmembers $m.distinguishedname $done
                                #$done += $m.displayname                               
                        }                      
                }
                $arr = New-Object system.Object
                $arr | Add-Member -memberType NoteProperty -name Name -value $item
                $arr | Add-Member -memberType NoteProperty -name Members -value $members
                return $arr
                #return $done
        } else {
                Write-Host "nested list:" $item        
        }      
}

function Get-DLPerms ([string]$item, [array]$done) {
# Pull out all accounts and groups that have perms to send to a specific list.
        $x += 1
        $members = @()
        if ((($done -match $item).count -eq 0) -or ($done.count -eq 0)) {
                $done += $item
                $tempDL = Get-DistributionGroup $item
                $mems = $tempDL.acceptmessagesOnlyFrom
                $mems += $tempDL.acceptmessagesOnlyFromDLMembers
                if ($mems.Count -ne 0){
                        foreach ($m in $mems) {
                                $members += $m
                                $t = Get-QADObject $m.distinguishedname                        
                                if ($t.type -ne "group") {
                                        Write-Host ("-"*$x) $t.displayname
                                } else {
                                        Write-Host ("-"*$x) $t.displayname "(group)"
                                        get-dlmembers $m.distinguishedname $done
                                        #$done += $m.displayname                               
                                }
                        }
                        $arr = New-Object system.Object
                        $arr | Add-Member -memberType NoteProperty -name Name -value $item
                        $arr | Add-Member -memberType NoteProperty -name Members -value $members
                        return $arr
                        #return $done
                } else {
                        Write-Host $item}
        } else {
                Write-Host "nested list:" $item
        }
}

$x=0
$done = @()
#Display name of the mailbox that I am looking for:
$findMbx = "Woodford, Eric"

#DL that I want to verify permissions on
$findDL = "All Eric's Family and Friends"

$find = Get-Mailbox -Identity $findmbx
if ($find -eq $null) {
        cls
        Throw "cound not find "+$findmbx}

$mems = Get-DLPerms $findDL $done
$Objects = $mems | %{$_.members}
$found = $Objects | ?{$_.DistinguishedName -eq $find.DistinguishedName}
Write-Host " ----------------------------- "
if ($found -ne $null) {
        Write-Host " " $findmbx " has permissions to send to " $finddl
} else {
        Write-Host " " $findmbx " does not have permissions to send to " $finddl
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.