Pro-Exchange,Lync & Office 365
Belgian Microsoft Unified Communications Professionals
Microsoft Exchange Server, Microsoft Lync Server & Office 365
Get a list of mobile devices in Exchange 2010 using PowerShell

Introduction

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:\Logs\ActiveSync-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:\Logs\ActiveSync-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:\temp\eas-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"
}

Again: thanks Tom for pointing this out!


Posted 09-27-2011 11:12 by Michael Van Horenbeeck

Comments

Bob@Bourns wrote re: Get a list of mobile devices in Exchange 2010 using PowerShell
on 07-06-2013 1:18

Why does your script result in clean CSV file that imports correctly into Excel, but Tom's CSV file does not separate the last two fields (useragent,lastsynctime) with a semicolon. This results with both concatenated under "useragent" and "lastsynctime" empty in the Excel.  Is the 'n' in Tom's LastSyncTime'n"  correct?  Removing it did not correct the problem.

Yours:

Add-Content -path $LogFile "name,devicemodel,devicetype,useragent,lastsynctime"

Tom's

"User;DeviceType;DeviceModel;DeviceID;DeviceUserAgent;LastSyncTime'n"

http://www.geschichten24.com/cgi-bin/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fgame-war-fire-age-hack%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Fgame-war-fire-age-hack%2F%3C%2Fa%3E wrote http://www.geschichten24.com/cgi-bin/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fgame-war-fire-age-hack%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Fgame-war-fire-age-hack%2F%3C%2Fa%3E
on 09-12-2014 12:08

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://aps-sn.net/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E wrote http://aps-sn.net/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E
on 09-13-2014 3:09

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

read the article wrote read the article
on 09-13-2014 2:39

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

clash of clans cheats wrote clash of clans cheats
on 09-13-2014 7:47

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://www.ts-solutions24.de/cgi-bin/info.php?a%5B%5D=hack+dragon+city+%28%3Ca+href%3Dhttp%3A%2F%2Fwww.socialcheats.com%2Fdragon-city-hack-dragon-city%2F%3Esocialcheats.com%3C%2Fa%3E%29 wrote http://www.ts-solutions24.de/cgi-bin/info.php?a%5B%5D=hack+dragon+city+%28%3Ca+href%3Dhttp%3A%2F%2Fwww.socialcheats.com%2Fdragon-city-hack-dragon-city%2F%3Esocialcheats.com%3C%2Fa%3E%29
on 09-14-2014 2:20

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

fifa 14 coin generator no survey wrote fifa 14 coin generator no survey
on 09-14-2014 6:53

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

E-Schmitz.Info wrote E-Schmitz.Info
on 09-14-2014 9:25

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://webmailalt.selfnet.at/?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Ffuturecheats.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Ffuturecheats.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E wrote http://webmailalt.selfnet.at/?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Ffuturecheats.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Ffuturecheats.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E
on 09-15-2014 3:43

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

fifa 15 coin generator wrote fifa 15 coin generator
on 09-15-2014 6:31

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Game Of War Fire Age Cheats wrote Game Of War Fire Age Cheats
on 09-16-2014 11:06

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://www.pikebu.de/cgi-bin/info.php?a%5B%5D=Fifa+15+Coin+Generator+%28%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%3C%2Fa%3E%29 wrote http://www.pikebu.de/cgi-bin/info.php?a%5B%5D=Fifa+15+Coin+Generator+%28%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%3C%2Fa%3E%29
on 09-16-2014 1:46

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

chun-teng.com.tw wrote chun-teng.com.tw
on 09-17-2014 6:11

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://195.244.166.11/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fracing-rivals-hack%2F%3ERacing+Rivals+Cheats%3C%2Fa%3E wrote http://195.244.166.11/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fracing-rivals-hack%2F%3ERacing+Rivals+Cheats%3C%2Fa%3E
on 09-17-2014 8:54

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Fifa 15 Coins For Playstation wrote Fifa 15 Coins For Playstation
on 09-17-2014 10:25

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Hack Call Of Duty Advanced Warfare wrote Hack Call Of Duty Advanced Warfare
on 09-19-2014 1:44

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Fifa 15 Coin Generator wrote Fifa 15 Coin Generator
on 09-21-2014 3:54

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Fifa 15 Coin Generator wrote Fifa 15 Coin Generator
on 09-21-2014 4:34

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://demo.membres.jexiste.org/?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fsims-4-cheats%2F%3ESims+4+Hack%3C%2Fa%3E wrote http://demo.membres.jexiste.org/?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fsims-4-cheats%2F%3ESims+4+Hack%3C%2Fa%3E
on 09-21-2014 5:12

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://www.sllist.ba/index.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E wrote http://www.sllist.ba/index.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3Ehttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3C%2Fa%3E
on 09-21-2014 12:40

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Free Gems For Android Clash Of Clans wrote Free Gems For Android Clash Of Clans
on 09-22-2014 6:47

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

How To Get Free Gems wrote How To Get Free Gems
on 09-24-2014 3:55

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://seec.no/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3EFifa+15+Coins+Glitch%3C%2Fa%3E wrote http://seec.no/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Ffifa-15-coin-generator%2F%3EFifa+15+Coins+Glitch%3C%2Fa%3E
on 09-25-2014 5:43

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

call of duty advanced warfare prestige hack wrote call of duty advanced warfare prestige hack
on 09-25-2014 10:09

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Google Play Gift Code Generator For Android wrote Google Play Gift Code Generator For Android
on 09-26-2014 1:09

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

Game Of War Fire Age Hack wrote Game Of War Fire Age Hack
on 09-27-2014 3:06

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

fifa 15 hack no downloads wrote fifa 15 hack no downloads
on 09-28-2014 4:01

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365

http://elisa.borec.cz/phpinfo.php?a%5B%5D=Racing+Rivals+Redeem+Codes+%28%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fracing-rivals-hack%2F%3ESuggested+Website%3C%2Fa%3E%29 wrote http://elisa.borec.cz/phpinfo.php?a%5B%5D=Racing+Rivals+Redeem+Codes+%28%3Ca+href%3Dhttp%3A%2F%2Fconfidencegames.com%2Fracing-rivals-hack%2F%3ESuggested+Website%3C%2Fa%3E%29
on 09-29-2014 7:50

Get a list of mobile devices in Exchange 2010 using PowerShell - Exchange 2010 - Pro-Exchange,Lync & Office 365