Get a list of mobile devices in Exchange 2010 using PowerShell
Hi there! Following my previous blog post about Exchange 2010 and consumerization of mobile devices, I decided to create a little script that might help you find out what mobile devices are actually connecting to your Exchange 2010 environment.
Step-by-Step
First, I define the variables that I’m going to use.
$Date = Get-Date -uformat “%Y%m%d”
$Logfile = “C:LogsActiveSync-all-$date.txt”
$Devices = @()
Secondly, I add headers to the text file. This makes handling easier when you import it into Excel.
Add-Content -path $LogFile “name,devicemodel,devicetype,useragent,lastsynctime”
Then we build a list of all mailboxes that have an ActiveSyncDevice configured (or in use) and we filter on objects that are on an Exchange 2010 server:
$Mailboxes = Get-CASMailbox -ResultSize Unlimited | Where {$_.HasActiveSyncDevicePartnership -eq $True -and $_.ExchangeVersion.ExchangeBuild.Major -ilike “14”}
Next, we loop through the result and per mailbox we’re going to query for devices using the Get-ActiveSyncDeviceStatistics cmdlet. We loop through each device to get some of the details (like the model etc):
ForEach ($mailbox in $mailboxes){
$Devices= Get-ActiveSyncDeviceStatistics -Mailbox $mailbox.name
$name = $mailbox.Name
ForEach ($device in $devices) {
$Model = $Device.DeviceModel
$Type = $Device.DeviceType
$LastSyncTime = $Device.LastSuccessSync
$UserAgent = $Device.DeviceUserAgent
Add-Content -path $Logfile “$name,$Model,$Type,$UserAgent,$LastSyncTime”
}
}
Bringing it all together
If we bring all of the above together, we get the following result:
$Date = Get-Date -uformat “%Y%m%d”
$Logfile = “C:LogsActiveSync-all-$date.txt”
$Devices = @()
Add-Content -path $LogFile “name,devicemodel,devicetype,useragent,lastsynctime”
$Mailboxes = Get-CASMailbox -ResultSize Unlimited | Where {$_.HasActiveSyncDevicePartnership -eq $True -and $_.ExchangeVersion.ExchangeBuild.Major -ilike “14”}
ForEach ($mailbox in $mailboxes){
$Devices= Get-ActiveSyncDeviceStatistics -Mailbox $mailbox.name
$name = $mailbox.Name
ForEach ($device in $devices) {
$Model = $Device.DeviceModel
$Type = $Device.DeviceType
$LastSyncTime = $Device.LastSuccessSync
$UserAgent = $Device.DeviceUserAgent
Add-Content -path $Logfile “$name,$Model,$Type,$UserAgent,$LastSyncTime”
}
}
The output of the file will look something like this:
Michael Van Horenbeeck,Android,Android,Android/3.2-EAS-1.2,09/27/2011 05:55:59,
Michael Van Horenbeeck,Android,Android,Android/3.2-EAS-1.2,,
Michael Van Horenbeeck,htcace,htcace,Android-EAS/3.10.000.083346.405,09/27/2011 20:00:48,
Michael Van Horenbeeck,TestActiveSyncConnectivity,TestActiveSyncConnectivity,Microsoft-Server-ActiveSync/12.0+(TestExchangeConnectivity.com),09/16/2011 13:05:41,
As you can see, I’m indeed using an Android-device. If you look closely, you’ll notice that there are 2 different devices.
You’ll also notice that not all devices register their Model/Type correctly (like my Android device). Others do pop out in the logs more clearly:
User X,iPad,iPad,Apple-iPad2C2/812.1,09/27/2011 18:52:48,
User X,HTC Touch Pro,PocketPC,MSFT-PPC/5.2.5310,09/27/2011 20:07:02,
User Y,iPad,iPad,Apple-iPad1C1/803.148,09/27/2011 20:12:39,
User Y,SAMSUNG,WP,MSFT-WP/7.0.7004,09/27/2011 20:19:11,
Note: If you ever used the testexchangeconnectivity.com website to test your ActiveSync deployment, you will notice that it will show up in the logs as well.
EDIT:
Tom Vergauwen, a colleague of me just pointed out there’s an easier way using:
Get-ActiveSyncDevice | Get-ActiveSyncDeviceStatistics
It requires less iterations in the script; making it a lot quicker to process (certainly in large environments).
Note: Get-ActiveSyncDevice is not available on Exchange 2007, so you’ll have to stick with the first script if you want to adapt it to Exchange 2007.
Bringing the best of both worlds together, results into the following script:
$Date = Get-Date -uformat “%Y%m%d”
$file = “C:tempeas-devicelist-$date.csv”
new-item $file -type file -force -value “User;DeviceType;DeviceModel;DeviceID;DeviceUserAgent;LastSyncTime’n”
$devices = Get-ActiveSyncDevice | Get-ActiveSyncDeviceStatistics
ForEach($device in $devices){
$Model = $Device.DeviceModel
$Type = $Device.DeviceType
$id = Device.DeviceID
$LastSyncTime = $Device.LastSuccessSync
$UserAgent = $Device.DeviceUserAgent
$identity = $device.identity|out-string
$identity = $identity.split(“/”)[-2]
Add-Content -Path $file “$identity;$Type;$Model;$id;$UserAgent$LastSyncTime”
}