Thursday 6 October 2016

Using PowerShell to Get Office 365 ActiveSync Device Information

I had a situation where I needed to get some device information about what was connected to my customer's current Office365 ActiveSync environment. Luckily after searching the Internet, I found this process to be rather simple, and I thought I would put my solution up on my blog to give an example of how to do it.

The code I put together focuses around using the get-mobiledevice commandlet rather than the older and eventually slated to be decommissioned get-activesyncdevice commandlet. I also added the export-csv commandlet to give me some output I could easily load into excel. If I put some extra work into it, I could make a native export to Excel, but I was doing a quick and dirty dump of the data.

To kick things off, we need to connect to Office365 so the following code I copied intact from TechNet: https://technet.microsoft.com/library/jj984289(v=exchg.160).aspx
*Make sure to open the PowerShell ISE or shell as an administrator.


Set-ExecutionPolicy RemoteSigned

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

You should be prompted with a login prompt to connect with your Office365 credentials at this point. The next line is where I pull out specific information about what is connected to ActiveSync.

get-mobiledevice | select deviceid,mobileoperator,id,deviceos,deviceimei,DeviceTelephoneNumber | export-csv C:\Activesync.csv

In the end, I make sure to disconnect my session if I decide to do further work with a different tenant.

Remove-PSSession $Session

I've added a link over here to the full script if you want to download and modify it to suit your needs.
https://1drv.ms/u/s!An-INXqoazfRkckHDEqeUc6lDxXyWw

Make sure to test out the get-mobiledevice commandlet as it can give you a wide range of device information such as:

OriginatingServer
IsValid
ObjectState
RunspaceId
FriendlyName
DeviceId
DeviceImei
DeviceMobileOperator
DeviceOS
DeviceOSLanguage
DeviceTelephoneNumber
DeviceType
DeviceUserAgent
DeviceModel
FirstSyncTime
UserDisplayName
DeviceAccessState
DeviceAccessStateReason
DeviceAccessControlRule
ClientVersion
ClientType
IsManaged
IsCompliant
IsDisabled
AdminDisplayName
ExchangeVersion
Name
DistinguishedName
Identity
Guid
ObjectCategory
ObjectClass
WhenChanged
WhenCreated
WhenChangedUTC
WhenCreatedUTC
OrganizationId
Id
OriginatingServer
IsValid
ObjectState