Identifying Exchange ActiveSync Users with PowerShell

Just recently, a problem involving the iPhone/iPod's synchronisation process with Exchange 2007, made it necessary for Exchange Administrators to alert various mail users to upgrade to the latest version of Apple's iOS 4. Ben shows how easily he was able to do this for his organisation, using Exchange's Management Shell with PowerShell and some Exchange cmdlets.

Almost two years ago, I wrote my first Simple-Talk article about reporting on mobile devices which connect to Exchange 2007. I still use the same process and script I described in that article to build weekly reports describing what type of devices are connecting to our Exchange environment. The data I have gathered has been very useful for analysing the use and growth of mobile device usage, as well as, on a more immediately practical note, helping to troubleshoot problems.

One of those problems came up a few weeks ago, when an issue was discovered with Apple devices running Apple’s iOS 4 operating system having problems syncing data from Microsoft Exchange 2007, and potentially causing performance problems on the Exchange servers themselves. Apple initially released an update for iOS 4 which changed some timeout settings to address the issue, and they later released iOS 4.0.1 which also included the fix. More details on the issue can be found on the Apple support site and the Exchange Team Blog.

The problem and fix are interesting (and relatively straightforward), but raise a couple of questions:

How can an Exchange administrator discover how many potentially affected devices are connecting to Exchange, and get a list of the users of those devices so that they can be informed about the update?

In my case, I was able to use my mobile device reporting database to quickly find the Apple device users I needed to contact, but what if I hadn’t had the database available – how then to get a list of devices of a specific type or with a specific client OS version? Fortunately, the Exchange Management Shell offers us all the tools we need to get this information.

What Devices are Connected?

The Get-ActiveSyncDeviceStatistics cmdlet is used to retrieve the mobile device information for a specific mailbox or device. To retrieve a list of all the mobile devices associated with my mailbox, I would use this command:


Figure 1. discovering all the devices associated with my exchange mailbox.

To find all the mobile devices in the entire organisation, we can use the Get-Mailbox cmdlet to retrieve all the mailboxes in Exchange, and then pass the results to the Get-ActiveSyncDeviceStatistics cmdlet to get the associated devices.

To reduce the number of mailboxes returned, we can use server-side filtering with the Get-Mailbox cmdlet to limit the results to user mailboxes (not rooms or resources) which are not hidden from the address book:

Then, to return all the mobile devices which are associated with the mailboxes:

(Depending on the number of mailboxes and devices this command may take a few minutes to complete.)

The Get-ActiveSyncDeviceStatistics cmdlet does not support server-side filtering, so although we can’t use server-side filters to reduce the amount of time the command takes, we can use client-side filters to limit the results to just the devices we are interested in. In this case, we’re interested in Apple devices, which all have a device type beginning “iP” (iPad, iPhone, iPod). To further limit the results, we can also filter for devices which have connected in the last 30 days:

It is also possible to find mobile devices using the Get-CASMailbox cmdlet, which supports server-side filtering with the filter “HasActiveSyncDevicePartnership -eq $true”:

This command should only return mailboxes which have mobile device partnerships, but in my testing many mailboxes which did not have mobile device partnerships still had HasActivesyncDevicePartnership set to true and were returned.

Once the results are returned, the $Devices variable will contain the details of all the devices we are interested in, and working with the result set is quick and easy. With some more PowerShell filtering, we can quickly get the information we are interested in. For example, to return the number of devices of each type:


Figure 2: Grouping the results according to device type.

To return the number of devices by User Agent (which, for Apple devices, gives software/hardware version):


Figure 3. Grouping the devices according to Software / Hardware version.

Narrowing the Search

To get a count of the number of devices of a specific version, in this case the Apple iPhone 4, there are a few things you need to know.

After upgrading an iPhone to iOS4, the DeviceUserAgent string starts “Apple-iPhone3” for the iPhone 4, “Apple-iPhone2” for the 3GS, and “Apple-iPhone1” for the 3G. In addition, devices running iOS 4.0 will end in “801.293“, and devices running iOS 4.0.1 will end in “801.306“. Devices running other versions of iOS use different strings. For an example of this, to get a count of all the iPhone 4 devices:


Figure 4. A count of all the iPhone 4 devices associated with Exchange mailboxes.

Now, back to the problem we started with – we want to send an e-mail to all Apple device users, asking them to install the update, and we can naturally exclude those already running iOS 4.0.1. This command will tell us how many slow upgraders there are:


Figure 5. A count of all the iPhone 4 devices which are associated with Exchange mailboxes but not running the latest version of the iOS software.

Next we need the e-mail addresses of the owners of these devices, which we can get by parsing the Identity attribute which is returned by the Get-ActiveSyncDeviceStatistics cmdlet. By looking at the Identity attribute of the device objects, we can see that it contains a property named “SmtpAddress“:


Figure 6. The list of properties returned by the Get-ActiveSyncDeviceStatistics cmdlet.

We can use this property to build our list of e-mail addresses, and by piping the output through the Get-Unique cmdlet we are ensuring that our list of addresses only contains once instance of each address (otherwise a user who had more than one device would appear multiple times):


Figure 7. A de-duplicated list of email addresses for the users we wish to contact.

The list of email addresses can be output to a file, copied from the shell and pasted into an e-mail message, or an e-mail message can be sent directly from PowerShell:

In our own case, once we had alerted our users, and they started to install the update on their devices, we immediately saw improvements on our Exchange servers, and many users reported that their devices were working better.

Get-ActiveSyncDeviceStatistics is just one of the cmdlets available for working with mobile devices. There are further cmdlets which will, for example, remove a device partnership, test connectivity, configure policy, and even send a remote wipe command. I’ll explore some of these in future Simple-Talk articles but, as always, there is documentation available at the TechNet website.

This article was commissioned by Red Gate Software, engineers of ingeniously simple tools for optimizing your Exchange email environment. Find out more.