Skip to main content

Exchange 2007/2010 iPhone activesync discovery script

Update ** There is one problem with the script in this post in that its doesn't pick up the updates to the O/S as pointed out in http://www.hedonists.ca/2010/07/22/blocking-the-iphone-part-i. So the only 100% way would be to parse the IIS logs where you can grab the same information about the devices. I'll try to post a script that does parsing that should work on 2003 up. Thanks also to Dennis who sent another version of the script I've added to the download that give enhanced reporting** ** New scripted posted for IIS logs http://gsexdev.blogspot.com/2010/08/parsing-iis-log-activesync-traffic-for.html **

On January 9, 2007 Apple released their first iphone and unleashed on the world what is arguably one of the most successful user mobile devices on the planet (I'm not arguing technical merit here just end user experience). Kind of like a virus these phones have spread throughout corporations and into every nook and cranny some with the IT departments blessing others with their scorn. Whether you like or hate the devices if your in charge of managing an Exchange server it can be useful to see how many of these devices are out there and what version of the Apple IOS they are running. This can help you keep abreast of any issues or limitations these devices might impose on your users or server.

On Exchange 2007/2010 getting information about what devices have been registered via ActiveSync is exceeding easy thanks to the Get-ActiveSyncDeviceStatistics cmdlet. With this cmdlet there are two distinct properties that are of interest when trying to report on iPhones. The first is the DeviceType property which gets set to iPhone for any iPhone. The second is the DeviceUserAgent which holds information about what version of IOS is running on the phone and in the later versions of the IOS holds information about what type of iPhone it is (eg 3G,3GS,4 etc).

Now this is where we strike the first problem like all good software the version information that you can get out of the DeviceUserAgent doesn't relate directly to the version of IOS the phone is running so some parsing and translation is needed. This is where the good old Internet helps out firstly on Wikipedia all the IOS versions have been documented at http://en.wikipedia.org/wiki/Apple_iOS_version_history . So this page gives us a IOS version to SDK build version this still doesn't quite get us to what is being stored in the deviceUserAgent property this needs to be translated with a little more code which looks like

$inparray = $inputvar.Split(',')
$v1 = $inparray[1].Substring(0,1)
$v2 = $inparray[1].Substring(1,1)
$v3 = $inparray[1].Substring(2,($inparray[1].Length-2))
$useragent = "{0:D2}" -f (([int][char]$v2)-64)

essentially this reformats the alpha character to a numerical represntation from 0-26. This then allows the IOS version to be matched. The actually phone version seems to only work on later version of the IOS and I've based it on information from a few forum posts so this is a little imcomplete I also didn't really have any information on how the iPad appears so this script i dont think will detect one but Im intrested in any feedback anyone has about other devices or codes they notice.

So what the rest of the script does is basically using the Get-Mailbox and Get-ActiveSyncDeviceStatistics cmdlet is it enumerates all iPhones activesync registrations for all users and uses some custom objects to build a Html report and then send it to a nomintated email address configured in the following varibles via SMTP using the configured server.

$sendAlertTo = "user@domain.com"
$sendAlertFrom = "Senderfromaddress@domain.com"
$SMTPServer = "servername"

These three varibles need to be configured before you will get a result out of the script an alternate method to sending the report as an email is just to output the result to a HTML file using Out-file eg you would modify the script like

$appleCollection | ConvertTo-HTML -head $tableStyle –body $body | out-file c:\iphreport.htm

I've put a download of this script here the code itself looks like

$sendAlertTo = "user@domain.com"
$sendAlertFrom = "Senderfromaddress@domain.com"
$SMTPServer = "servername"

$appleCollection = @()
$appleverhash = @{ }
$hndSethash = @{ }

function addtoIOShash($inputvar){
$inparray = $inputvar.Split(',')
$v1 = $inparray[1].Substring(0,1)
$v2 = $inparray[1].Substring(1,1)
$v3 = $inparray[1].Substring(2,($inparray[1].Length-2))
$useragent = "{0:D2}" -f (([int][char]$v2)-64)
$apverobj = "" | select IOSVersion,IOSReleaseDate,ActiveSyncUserAgent,AppleBuildCode
$apverobj.IOSVersion = $inparray[0]
$apverobj.IOSReleaseDate = $inparray[2]
$apverobj.AppleBuildCode = $inparray[1]
$apverobj.ActiveSyncUserAgent = $v1 + $useragent + "." + $v3
$appleverhash.add($apverobj.ActiveSyncUserAgent,$apverobj)

}



#IOSVersion,Deviceid,ReleaseDate
addtoIOShash("1,1A543,Jun-07")
addtoIOShash("1.0.1,1C25,Jul-07")
addtoIOShash("1.0.2,1C28,Aug-07")
addtoIOShash("1.1,3A100,Sep-07")
addtoIOShash("1.1,3A101,Sep-07")
addtoIOShash("1.1.1,3A109,Sep-07")
addtoIOShash("1.1.1,3A110,Sep-07")
addtoIOShash("1.1.2 ,3B48,Nov-07")
addtoIOShash("1.1.3,4A93,Jan-08")
addtoIOShash("1.1.4,4A102,Feb-08")
addtoIOShash("1.1.5,4B1,Jul-08")
addtoIOShash("2,5A347,Jul-08")
addtoIOShash("2.0.1,5B108,Aug-08")
addtoIOShash("2.0.2,5C1,Aug-08")
addtoIOShash("2.1,5F136,Sep-08")
addtoIOShash("2.1,5F137,Sep-08")
addtoIOShash("2.1,5F138,Sep-08")
addtoIOShash("2.2,5G77,Nov-08")
addtoIOShash("2.2.1,5H11,Jan-09")
addtoIOShash("3,7A341,Jun-09")
addtoIOShash("3.0.1,7A400,Jul-09")
addtoIOShash("3.1,7C144,Sep-09")
addtoIOShash("3.1,7C145,Sep-09")
addtoIOShash("3.1,7C146,Sep-09")
addtoIOShash("3.1.2,7D11,Oct-09")
addtoIOShash("3.1.3,7E18,Feb-09")
addtoIOShash("3.2,7B367,Apr-10")
addtoIOShash("4.0,8A293,Jun-10")


$hndSethash.add("1C2","IPhone 3G")
$hndSethash.add("2C1","IPhone 3GS")
$hndSethash.add("3C1","IPhone 4")

Get-Mailbox -ResultSize Unlimited | ForEach-object {
$Mb = $_
Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity | foreach-object {

if($_.DeviceType -eq "iPhone"){
$userRepobj = "" | select UserName,emailAddress,IphoneType,IOSVersion,IOSReleaseDate,ActiveSyncUserAgent,ActiveSyncDeviceUserAgent,AppleBuildCode
if ($_.DeviceUserAgent -ne $null){
$apcode = $_.DeviceUserAgent
$userRepobj.ActiveSyncDeviceUserAgent = $_.DeviceUserAgent
$apcodearray = $apcode.split("/")
if ($apcodearray[0].length -gt 12){
$userRepobj.IphoneType = $hndSethash[$apcodearray[0].Substring(12,3)]
}
$userRepobj.IOSVersion = $appleverhash[$apcodearray[1]].IOSVersion
$userRepobj.IOSReleaseDate = $appleverhash[$apcodearray[1]].IOSReleaseDate
$userRepobj.ActiveSyncUserAgent = $appleverhash[$apcodearray[1]].ActiveSyncUserAgent
$userRepobj.AppleBuildCode = $appleverhash[$apcodearray[1]].AppleBuildCode
$userRepobj.UserName = $Mb.Name
$userRepobj.emailAddress = $Mb.WindowsEmailAddress
$appleCollection += $userRepobj
}
}
}

}

$tableStyle = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
}
TH{border-width: 1px;
padding: 10px;
border-style: solid;
border-color: black;
background-color:#66CCCC
}
TD{border-width: 1px;
padding: 2px;
border-style: solid;
border-color: black;
background-color:white
}
</style>
"@

$body = @"
<p style="font-size:25px;family:calibri;color:#ff9100">
$TableHeader
</p>
"@



$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $SMTPServer
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.To.Add($sendAlertTo)
$MailMessage.From = $sendAlertFrom
$MailMessage.Subject = "iPhone Registrration Report"
$MailMessage.IsBodyHtml = $TRUE
$MailMessage.body = $appleCollection | ConvertTo-HTML -head $tableStyle –body $body
$SMTPClient.Send($MailMessage)

Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.