Thursday, July 15, 2010

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


haaw said...

Great Script Glen.

Is there an easy way of selecting a particular Exchange Server? We have a Staff and Student Exchange system in our environment and would only like the Accounts checked on the Staff Exchange servers.

Elsewhere I have listed these server and looped through a command for each of these servers but would like to know if there is a better solution.

Thanks again for your awesome scripts.

Anonymous said...

Haaw, Get-Mailbox will let you refine the scope of the mailbox results, it's in this section.

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

Narendra said...

I was not able to execute this script, was getting the error "Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently."

Had to change the code to
$Mailboxes = Get-Mailbox -ResultSize Unlimited
$Mailboxes | ForEach-object {

to make it work

Juan said...

Thanks for the script Glen. Seems to be very useful. Unfortunately it doesn't seem to work for my environment. Exchange 2007 SP2 servers - clustered scc. I get an error message right from the start of index failed;the array index evaluated to null afterwards I see another error saying "You cannot call a method on a null-valued expression."

I would greatly appreciate any assistance with this. Not a powershell programmer although I usually can follow the code good to a point.

Thanks in advance,


Juan said...

Never mind I fixed it. I works great. thanks for shared this script and all others on your site.
Take Care.


Anonymous said...

Hi Glen,
great thanks for this awesome script which provide us the information we want to have. I've tried to add the LastSuccessSync to the script, to identify old iPhone's - but the row is still empty. Can you assist me to get this information filled?

Best regards,

Glen said...

Hi Marcus I've updated the download with a modified script someone sent me that should get this information. The other things i wanted to say is there is one major problem with this approach is 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 have most of a script that will do this put together that i hope should work against 2003/2007 and 2010 although log parsing is a little slow.

Rob said...


This script is awesome, I need a bit of help in that it is not pulling the iPads, or iPods. Is this possible in this script? I added the following to your script but no luck (shown below). Plus the old iphone's are not reporting yet it is giving me all the info just not labling in the iphone type.

Any help would be greatly appreciated..

$hndSethash.add("1C2","iPhone 3G")
$hndSethash.add("2C1","iPhone 3GS")
$hndSethash.add("3C1","iPhone 4")
$hndSethash.add("Apple-iPod","iPod Touch")

Glen said...

I dont have any of those devices to test with the script filter with the following line

if($_.DeviceType -eq "iPhone")

so if the devicetype is different for these devices it wont be included you would may add other devices with a -bor eg

if($_.DeviceType -eq "iPhone" -bor $_.DeviceType -eq "freedofrog")

as long as you can work out what the devicetype settings are for those devices.


Glen said...

I think you might need also need

$hndSethash.add("Apple-iPhone1C2","iPhone 3G")
$hndSethash.add("Apple-iPhone2C1","iPhone 3GS")
$hndSethash.add("Apple-iPhone3C1","iPhone 4")
$hndSethash.add("Apple-iPod2C1","iPod Touch 2G")
$hndSethash.add("Apple-iPod3C1","iPod Touch 3G")

Rob said...


After using this which works perfectly

if($_.DeviceType -eq "iPhone" -bor $_.DeviceType -eq "freedofrog")

I get the following powershell errors

Exception calling "Substring" with "2" argument(s): "Index and length must refe
r to a location within the string.
Parameter name: length"
At C:\scripts\iphone\iphv9.ps1:87 char:68
+ $userRepobj.IphoneType = $hndSethash[$apcodearray[0].Subs
tring( <<<< 12,3)]

Also in your html formated output, for the iphonetype what does it pull from in your code, because only a few builds don't show up. and example is the following.

3.1.3 Feb-09 705.18 Apple-iPhone/705.18 7E18

I do have all the addtoIOShash

Thanks again!!