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 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 **

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 . 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 = ""
$sendAlertFrom = ""
$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 = ""
$sendAlertFrom = ""
$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


addtoIOShash("1.1.2 ,3B48,Nov-07")

$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 = @"
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;
TD{border-width: 1px;
padding: 2px;
border-style: solid;
border-color: black;

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

$SmtpClient = new-object
$ = $SMTPServer
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.From = $sendAlertFrom
$MailMessage.Subject = "iPhone Registrration Report"
$MailMessage.IsBodyHtml = $TRUE
$MailMessage.body = $appleCollection | ConvertTo-HTML -head $tableStyle –body $body

Popular posts from this blog

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 '

A walk-though using the Graph API Mailbox reports in Powershell

Quite recently the Reporting side of the Graph API has moved in GA from beta, there are quite a number of reports that can be run across various Office365 surfaces but in this post I'm going to focus on the Mailbox related ones. Accessing Office365 Reports using Powershell is nothing new and has been available in the previous reporting endpoint however from the end of January many of these cmdlets are now being depreciated in favour of the Graph API . Prerequisites  In comparison to using the Remote PowerShell cmdlets where only the correct Office365 Admin permissions where needed, to use the new Graph API reports endpoint you need to use OAuth for authentication so this requires an Application Registration  that is then given the correct oAuth Grants to use the Reports EndPoin

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 .  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
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.