Click here to monitor SSC
Av rating:
Total votes: 31
Total comments: 2


Ben Lye
Identifying Exchange ActiveSync Users with PowerShell
26 August 2010

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:

Get-ActiveSyncDeviceStatistics -Mailbox blye |
    ft DeviceType, DeviceUserAgent, LastSuccessSync

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:

$Mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox –ResultSize Unlimited `

    -Filter {HiddenFromAddressListsEnabled -eq $false}

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

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

$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”:

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

$Devices | Group-Object -Property DeviceType -NoElement

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

$Devices | Group-Object -Property DeviceUserAgent –NoElement

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:

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

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:

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

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

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

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:

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



This article has been viewed 20027 times.
Ben Lye

Author profile: Ben Lye

Ben Lye is a senior systems administrator at a multi-national software company. He has over 10 years experience supporting and administering Windows and Exchange, and has been MCSE and MCP certified since 1999. Ben is passionate about automating and streamlining routine tasks, and enjoys creating and using tools which make day-to-day administration easier.

Search for other articles by Ben Lye

Rate this article:   Avg rating: from a total of 31 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Hi
Posted by: Thaister (view profile)
Posted on: Sunday, October 10, 2010 at 8:00 PM
Message: Hi,
Im trying to retrieve list of users that has activesync enable and running for the last 90 days... can you help me how to do it? using powershell command?

Thanks

Subject: Message size
Posted by: mohammed2010 (view profile)
Posted on: Sunday, October 17, 2010 at 12:56 AM
Message: Dear ben lye.
mohammed with you dear, i am beginner in exchange. i have one doubt. could you clarify me. E2K7 with hub transport role. how can i check the default size of user mailbox.. can you please reply me ASAP. Thanks in advance.

 





How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating to Microsoft BPOS - Part II
 In his last article, Johan gave us a crystal clear guide to preparing to migrate from an on-premises... Read more...

Monitoring Mailbox Moves
 Mailboxes moves happen all the time, and given how precious the data in mailboxes can be, you should... Read more...

Emulating the Exchange 2003 RUS for Out-of-Band Mailbox Provisioning in Exchange 2007
 Exchange's Recipient Update Service was important in Exchange 2000 or 2003 in order to complete the... Read more...

The Postmasters
 The Exchange Team introduces themselves, and keeps you up-to-date Read more...

Upgrade Exchange 2003 to Exchange 2010
  In this article, the first of two in which Jaap describes how to move from Exchange Server 2003... Read more...

Upgrade Exchange 2003 to Exchange 2010 - Part II
 In Jaap's second article on upgrading straight from Exchange Server 2003 to 2010, he explains how to... Read more...

Goodbye Exchange ExMerge, Hello Export-Mailbox
 ExMerge was a great way of exporting a mailbox to an Exchange PST file, or for removing all occurences... Read more...

Using Exchange 2007 for Resource Booking
 The process of booking various resources to go with a meeting room just got a whole lot easier with... Read more...

Managing Exchange 2007 Mailbox Quotas with Windows PowerShell
 The use of PowerShell with Exchange Server 2007 can do a great deal to ease the task of managing... Read more...

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk