2

Get Mobile Device Statistics In Exchange Online Using Powershell

Recently I was checking out my Azure tenant to see which mobile devices are currently in play. This reason for this was to see if there were any stale devices that can be deleted or if there were devices of a certain type that I wanted to filter. For this I would use the Get-MobileDevice and Get-MobileDeviceStatistics cmdlets to, well… Get Mobile Device Statistics in Powershell.

Prerequisites

One of the prerequisites you’ll need to in order to make this happen is to install the Exchange Online module. Luckily it’s not too difficult if you follow the linked article. Another requirement is the account you’re using must have the Exchange Administrator role to be able to query Exchange Online.

So in short, here is what you’ll need.

  • Exchange Online Management Module
  • Exchange Administrator or Global Administrator Role

Exchange Admin Role in Azure
 

Get Mobile Device Statistics Using Powershell

Alright now that we know what we need, let’s look at how to extract that information from Exchange Online. As mentioned we will be using the Get-MobileDevice cmdlet along with the Get-MobileDeviceStatistics to get the different properties. Get-MobileDevice has a mailbox parameter so we can filter devices that are associated with a mailbox, assuming you only wanted a single user’s device. Let’s look at what that looks like in the shell.

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName [email protected]

Get-MobileDevice -Mailbox pcontreras | select @{Name = 'Identity' ; Expression = {$_.Identity -replace "\\.+"}}, `
DeviceId, IsManaged, IsCompliant, DeviceOS, DeviceType, FriendlyName, `
DeviceUserAgent, FirstSyncTime, DeviceAccessState, DeviceAccessStateReason, ClientType -First 1

Identity                : Paul Contreras
DeviceId                : D1151F256364422E827C79AF3D3B2230
IsManaged               : False
IsCompliant             : False
DeviceOS                : iOS 13.7
DeviceType              : Outlook
FriendlyName            :
DeviceUserAgent         : Outlook-iOS/2.0
FirstSyncTime           : 9/12/2020 2:43:42 AM
DeviceAccessState       : Allowed
DeviceAccessStateReason : ExternallyManaged
ClientType              : Outlook

Get-MobileDevice cmdlet output
 

Get Mobile Device Statistics show similar results, however, I do like the fact that it has a LastSuccessSync and LastSyncAttemptTime to see exactly how long devices have been stale. If you’re really good with with regex, you can parse the text in the Identity property to give you just the name. My regex game is really weak so if you know how to do that, please post it in the comments so other viewers can use it as well.

Powershell Script To Get Device and Statistics

Since I like to use these 2 cmdlets interchangeably, I thought it would be helpful to provide a script so you can call the Get-MobileDeviceStatistics using the GUID from Get-MobileDevice output.

#Get all mobile devices in the org
$MobileDeviceList = Get-MobileDevice

#Alternatively, Get all mobile devices from a single user
$MobileDeviceList = Get-MobileDevice -Mailbox [email protected]

foreach ($Device in $MobileDeviceList) {
    $Stats = Get-MobileDeviceStatistics -Identity $Device.Guid.toString()
    [PSCustomObject]@{
        Identity              = $Device.Identity -replace "\\.+"
        DeviceType            = $Device.DeviceType
        DeviceOS              = $Device.DeviceOS
        LastSuccessSync       = $Stats.LastSuccessSync
        LastSyncAttemptTime   = $Stats.LastSyncAttemptTime
        LastPolicyUpdateTime  = $Stats.LastPolicyUpdateTime
        LastPingHeartbeat     = $Stats.LastPingHeartbeat
        ClientType            = $Stats.ClientType
    }
}

Identity             : Paul Contreras
DeviceType           : Outlook
DeviceOS             : iOS 13.7
LastSuccessSync      : 9/12/2020 2:43:43 AM
LastSyncAttemptTime  : 9/12/2020 2:43:43 AM
LastPolicyUpdateTime : 9/12/2020 2:43:43 AM
LastPingHeartbeat    :
ClientType           : Outlook

Identity             : Paul Contreras
DeviceType           : iPhone
DeviceOS             : iOS 14.1 18A8395
LastSuccessSync      : 11/12/2020 2:40:02 AM
LastSyncAttemptTime  : 11/12/2020 2:40:02 AM
LastPolicyUpdateTime : 11/11/2020 6:32:40 PM
LastPingHeartbeat    : 600
ClientType           : EAS

Get Mobile Device Statistics Function

 

Alright folk, hopefully this article was useful enough to provide you the information needed to get mobile device statistics using Powershell. Furthermore, I hope it was enough to determine which of those devices are stale and no longer in use. This is an excellent strategy for when you want to do some spring cleaning.

If you like to see more cloud content, be sure to check out our Office 365 Cloud Category for more useful tips and tricks. Finally, be sure to stop by our Youtube Page for those visual learners.

5/5 - (8 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

2 Comments

  1. I have the same question as Steven. How do you do this for all “user” mailboxes in EXO? Also, the Get-MobileDeviceStatistics command should be Get-EXOMobileDeviceStatistics according to Microsoft. I have a script to loop through devices and get the information I want so that I can verify it before deleting old devices, but I keep getting errors on some stating is matches multiple entries. I don’t trust the data it gives me if the script has errors. Any ideas?
    Import-Module ExchangeOnlineManagement
    $Admin = Read-Host “Please enter your administrative user account email”

    Connect-ExchangeOnline -UserPrincipalName $Admin

    $DevToRm = Get-MobileDevice -ResultSize Unlimited | Get-EXOMobileDeviceStatistics | Select-Object DeviceID, Identity, LastSuccessSync, Guid | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-30”)}
    $DevToRm | Export-Csv “c:\Temp\Mobile_DevicestoRemove.csv”

    Disconnect-ExchangeOnline
    Get-EXOMobileDeviceStatistics : An error occurred while processing this request.. {“error”:{“code”:”InternalServerError”,”message”:”Error executing
    request. The operation couldn’t be performed because ‘UserNameHidden\\ExchangeActiveSyncDevices\\Hx\u00a7Outlook\u00a74D3328FAD5BF3716A733E6F747C9CC76’
    matches multiple
    entries.”,”details”:[{“code”:”Context”,”target”:””,”message”:”Ex9E65A2|Microsoft.Exchange.Configuration.Tasks.ManagementObjectAmbiguousException|The
    operation couldn’t be performed because ‘User.Name\\ExchangeActiveSyncDevices\\Hx\u00a7Outlook\u00a74D3328FAD5BF3716A733E6F747C9CC76’ matches
    multiple entries.”}],”innererror”:{“message”:”Error executing request. The operation couldn’t be performed because ‘User.Name\\ExchangeActiveSyncDevices\\Hx\u00a7Outlook\u00a74D3328FAD5BF3716A733E6F747C9CC76’ matches multiple
    entries.”,”type”:”Microsoft.Exchange.Admin.OData.Core.ODataServiceException”,”stacktrace”:” at
    Microsoft.Exchange.AdminApi.PSDirectInvokeProvider.AdminDirectInvokeProviderBase`3.Handl …
    At line:6 char:53
    + … eDevice -ResultSize Unlimited | Get-EXOMobileDeviceStatistics | Selec …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ProtocolError: (:) [Get-EXOMobileDeviceStatistics], Exception
    + FullyQualifiedErrorId : One or more errors occurred.,Microsoft.Exchange.Management.RestApiClient.GetExoMobileDeviceStatistics

Leave a Reply

Your email address will not be published.