Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (2024)

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.

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:

1

2

Get-ActiveSyncDeviceStatistics -Mailbox blye |

ft DeviceType, DeviceUserAgent, LastSuccessSync

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (1)

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:

1

$Devices = $Mailboxes | %{Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity}

(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:

1

2

$Devices = $Mailboxes | %{Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity} | `

?{$_.DeviceType -like "iP*" -and $_.LastSuccessSync -gt (Get-Date).AddDays(-30)}

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

1

$Mailboxes = Get-CASMailbox -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:

1

$Devices | Group-Object -Property DeviceType -NoElement

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (2)

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):

1

$Devices | Group-Object -Property DeviceUserAgent -NoElement

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (3)

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

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:

1

$Devices | ?{$_.DeviceUserAgent -like "Apple-iPhone3*"} | Measure-Object

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (4)

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:

1

$Devices | ?{$_.DeviceUserAgent -notlike "*801.306"}| Measure-Object

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (5)

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“:

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (6)

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):

1

$Addresses = $Devices | ?{$_.DeviceUserAgent -notlike "*801.306"} | %{$_.Identity.SmtpAddress} | Get-Unique

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (7)

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

# Change these variables to suit your own environment and needs

$SMTPServer = "smtp.example.com"

$SMTPPort = 25

$Subject = "Please Update Your Mobile Device"

$Body = "IT requires you to update the operating system on your mobile device."

$From = "admin@example.com"

# Send an e-mail to each e-mail address in $Addresses

$Addresses | % {

$Mail = (New-Object System.Net.Mail.smtpclient($SMTPserver, $SMTPport))

$Mail.Send($From, $_, $Subject, $Body)

$Mail = $null

}

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.

Identifying Exchange ActiveSync Users with PowerShell - Simple Talk (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5862

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.