Exchange Script: Find ActiveSync Device Statistics for users in a Distribution Group

Scenario: You want to Find ActiveSync Device Statistics for users that are in a Distribution Group. If the Distribution Group does not contain members, it will not send the email. If it contains members, it will send an email for each member. Copy the content below and paste it into a .ps1 file and execute from Exchange Mangaement Shell.   

$mbox = Get-DistributionGroup "group-name"| Get-DistributionGroupMember
If ($mbox -ne $null)
{
$email = $mbox | ForEach {
$name = $_ | Select Name |Out-String
$body = get-activesyncdevicestatistics -mailbox $_.name | Sort DeviceFriendlyName | FT DeviceFriendlyName, DeviceModel, LastSyncAttemptTime, LastSuccessSync | Out-string
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "servername"
$mailmessage.from = ("EASMonitoring@domain.com")
$mailmessage.To.add("easstatistics@domain.com")
$mailmessage.Subject = "EAS Statistics"
$mailmessage.Body = "
EAS Statistics for:$name
$body
"
$smtpclient.Send($mailmessage)
}
}