###############################################################################
#Script to pull mobile devices for all Office 365 Users in tenant #
#Caleb Hengeveld #
#6/1/2019 #
###############################################################################
#### Connect to Exchange Online
$credentials = Get-Credential
Write-Output "Getting the Exchange Online cmdlets"
$session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-ConfigurationName Microsoft.Exchange -Credential $credentials `
-Authentication Basic -AllowRedirection
Import-PSSession $session
####Setting Variables
$csv = "C:\temp\MobileDevices.csv"
$results = @()
$mailboxUsers = get-mailbox -resultsize unlimited
$mobileDevice = @()
####Cycle through each mailbox looking for mobile devices
foreach($user in $mailboxUsers)
{
$UPN = $user.UserPrincipalName
$displayName = $user.DisplayName
$mobileDevices = Get-MobileDevice -Mailbox $UPN
foreach($mobileDevice in $mobileDevices)
{
Write-Output "Getting info about a device for $displayName"
$properties = @{
Name = $user.name
UPN = $UPN
DisplayName = $displayName
FriendlyName = $mobileDevice.FriendlyName
ClientType = $mobileDevice.ClientType
ClientVersion = $mobileDevice.ClientVersion
DeviceId = $mobileDevice.DeviceId
DeviceMobileOperator = $mobileDevice.DeviceMobileOperator
DeviceModel = $mobileDevice.DeviceModel
DeviceOS = $mobileDevice.DeviceOS
DeviceTelephoneNumber = $mobileDevice.DeviceTelephoneNumber
DeviceType = $mobileDevice.DeviceType
FirstSyncTime = $mobileDevice.FirstSyncTime
UserDisplayName = $mobileDevice.UserDisplayName
}
$results += New-Object psobject -Property $properties
}
}
####Pushing results to csv defined in variables
$results | Select-Object Name,UPN,FriendlyName,DisplayName,ClientType,ClientVersion,DeviceId,DeviceMobileOperator,DeviceModel,DeviceOS,DeviceTelephoneNumber,DeviceType,FirstSyncTime,UserDisplayName | Export-Csv -notypeinformation -Path $csv
#### Remove session to Exchange Online
Remove-PSSession $session