Find All Active Distribution Groups

As a precursor to a larger article I am writing, I wanted to at least get this little bit of code out to people and hope it is found to be useful. I’ve create a script that will examine all message tracking logs for the past month on multiple servers to find any distribution group that has received email in the past month. This script will store a CSV file for later use while also emailing it to the user of your choice. Here is the code:

# Set current date and one month back
$current =(get-date)
$OneMonth=(($current).addmonths(-1))

# Get all Exchange Transport Servers and setup array variables 
$servers = get-transportserver
$track1 = @() # Used as temp storage of tracking log information for one server
$track2 = @() # Used as temp storage of tracking log information for all servers

# process the logs for multiple servers
foreach ($name in $servers) {
                $track1 = Get-MessageTrackingLog -server $name.name -EventId Expand -ResultSize Unlimited -start $onemonth -end $current | select-object RelatedRecipientAddress | group-object relatedrecipientaddress | sort-object name
                $Track2 += @($track1)
}
 
$track = $track2 | group-object name # remove duplicates

# Export to CSV file
$rows = "Name"
Add-Content  \\server\share\ActiveGroups.csv $rows
foreach ($line in $track) {
                $name = (get-distributiongroup -identity $line.name).name
                $rowline = "$name"
                Add-Content \\server\share\ActiveGroups.csv $rowline
}
 
# Send email notification
[string]$ToAddressMaster = "<IT email address>" # My email address
[string]$FromAddressMaster = "<Reporting email address" # Windows Reporting email address
Send-MailMessage -to $ToAddressMaster -from $FromAddressMaster -subject "Active Distribution Groups - Past Month" -SmtpServer <smtp server> -attachment "\\server\share\ActiveGroups.csv"

I used hints from here and applied it to multiple servers.

Related TechNet Articles
Managing Distribution Groups
Semd-MailMessage

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s