Friday, May 23, 2014

Monitoring Exchange ActiveSync connection for specific users

To prevent a situation where the company managers have problems with reading mail from mobile devices, it is desirable to know about problems in advance. This can be done for example in the following script, which sends e-mail the last known good synchronization and the difference with the current time. If the time difference is greater than 3:00 script highlighted.
In this script, you need to replace the following values
"MyManager", "MyDirector" - Display Name from Active Directory
$msg.From, $msg.To.add, $SMTPServer - Your SMTP parameters

Download monitor_active_sync.ps1

# Load Exchange commands
if (!(Get-Command Get-ExchangeServer -ErrorAction SilentlyContinue))
{
    if (Test-Path "C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1")
    {
        . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
        Connect-ExchangeServer -auto
    }
    elseif (Test-Path "C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1")
    {
        Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin
        .'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'
    }
    else
    {
        throw "Exchange Management Shell cannot be loaded"
    }
}

$DeltaTime = $DateNow = $MobileDevices = @()

$DateNow = Get-date
$MobileDevices = Get-ActiveSyncDevice | where { $_.identity -match "MyManager" -or $_.identity -match "MyDirector" } | Sort-Object identity | Get-ActiveSyncDeviceStatistics

$msg = new-object Net.Mail.MailMessage
$msg.Body = "<html> 
<body> 
<font size=""1"" face=""Arial,sans-serif""> 
<h3 align=""center"">Notice about ActiveSync</h3> 
</font><table border=""0"" cellpadding=""3"" style=""font-size:8pt;font-family:Arial,sans-serif""> 
<tr bgcolor=""#009900""> 
<th><font color=""#ffffff"">Identity:</font></th> 
<th><font color=""#ffffff"">DeviceUserAgent:</font></th> 
<th><font color=""#ffffff"">LastSuccessSync GMT:</font></th> 
<th><font color=""#ffffff"">DeltaTime:</font></th> 
</tr>"

Foreach ($MobileDevice in $MobileDevices)
{
    write-host $MobileDevice.Identity "User Agent: " $MobileDevice.DeviceUserAgent "Success Sync GMT: "  $MobileDevice.LastSuccessSync
    if ($MobileDevice.LastSuccessSync)
    {
        $DeltaTime = new-timespan -start ($DateNow.ToUniversalTime()) -end ($MobileDevice.LastSuccessSync)
        write-host "Last sync delta time" $DeltaTime
    }
    else { write-host "No sync" }
    if ($DeltaTime -le '-3:00:00.0000000') { write-host "DeltaTime great 3 hour" }
    $msg.Body += "<tr bgcolor=""#dddddd""><th>$($MobileDevice.Identity)</th>
<th>$($MobileDevice.DeviceUserAgent)</th>
<th>$($MobileDevice.LastSuccessSync)</th>"
    if ($DeltaTime -le '-3:00:00.0000000') { $msg.Body += "<th><b><span style=""color: red;"">$($DeltaTime)</span></b></th>
</tr>"
    }
    else { $msg.Body += "<th>$($DeltaTime)</th>
</tr>"
    }
}

$msg.Body += "</table>
</body></html>"

$msg.Body | Out-File .\report.html
$msg.IsBodyHTML = $true
$msg.From = "FromEmail@YourDomain"
$msg.To.add("YourEmail@YourDomain")
$msg.Subject = "Notice about ActiveSync"
$SMTPServer = "YourEmailServer"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($Msg)

No comments:

Post a Comment