Powershell to find active sync devices and users

I am using this :
Get-Mailbox -ResultSize:Unlimited | ForEach {Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity} | Sort-Object -Property DeviceType,Identity | Select-Object @{name="EmailAddress";expression={$_.Identity.ToString().Split("\")[0]}},Devicetype,DeviceOS,LastSuccessSync

and it work for pulling the information I would like to add the DisplayName to this output but having issues getting to run and display all devices by DisplayName and EMail Address.
Thanks
McLovinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amit KumarCommented:
Try this one:

foreach ($mbx in Get-Mailbox -ResultSize Unlimited) {Get-ActiveSyncDeviceStatistics -Mailbox $MBX.Samaccountname | Select-Object @{name="DisplayName";expression={$MBX.Name}},@{name="EmailAddress";expression={$MBX.PrimarySMTPAddress}},Devicetype,DeviceOS,LastSuccessSync,DeviceModel} 

Open in new window


In case you want to get report in excel file with all details so try below one after saving in PS1.

<#---------------------------------------------------------------------
	.SYNOPSIS
		You can run this script with two switches as below mentioned

For specific user: .\Get-ActiveSyncDeviceReport.ps1 -Identity <username>
For All : .\Get-ActiveSyncDeviceReport.ps1 -all
-----------------------------------------------------------------------#>

param(
	[Parameter(ParameterSetName='Identity')] [string]$Identity,
	[Parameter(ParameterSetName='all')] [switch]$all
)

$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
$report = @()


#Set recipient scope
Set-ADServerSettings -ViewEntireForest $true

if ($filename)
{
	$reportfile = $filename
}
else
{
	$timestamp = Get-Date -UFormat %Y%m%d-%H%M
	$random = -join(48..57+65..90+97..122 | ForEach-Object {[char]$_} | Get-Random -Count 6)
	$reportfile = "ASReport-$timestamp-$random.csv"
}

#...................................
# Script
#...................................


if($all) 
	{ 
	$mbx = @(Get-CASMailbox -resultsize Unlimited | where{$_.HasActiveSyncDevicePartnership -eq "True"} | get-recipient) 
	}

if($Identity) 
	{ 
	$mbx = @(Get-CASMailbox $Identity | where{$_.HasActiveSyncDevicePartnership -eq "True"} | get-recipient)
	}

#Add dependencies
Import-Module ActiveDirectory

foreach ($mbx1 in $mbx)
{$aduser = Get-ADUser $mbx1.samaccountname -Properties Enabled
foreach($ast in (Get-ActiveSyncDeviceStatistics -mailbox $aduser.UserPrincipalName | where {$_.LastSuccessSync -gt (get-date).AddDays(-60)}))
{
$aduserstat = $aduser.enabled

	$userObj = New-Object PSObject
	$userObj | Add-Member NoteProperty -Name "Name" -Value $mbx1.name
	$userObj | Add-Member NoteProperty -Name "E-mailAddresss" -Value $mbx1.PrimarySMTPAddress
	$userObj | Add-Member NoteProperty -Name "Country" -Value $mbx1.CountryorRegion

if ($aduserstat -eq $True)
{
	$userObj | Add-Member NoteProperty -Name "Account Status" -Value "Enabled"
}
elseif ($aduserstat -eq $False)
{
	$userObj | Add-Member NoteProperty -Name "Account Status" -Value "Disabled"
}
	$userObj | Add-Member NoteProperty -Name "DeviceId" -Value $ast.DeviceId
	$userObj | Add-Member NoteProperty -Name "DeviceOS" -Value $ast.DeviceOS
	$userObj | Add-Member NoteProperty -Name "DeviceModel" -Value $ast.DeviceModel
	$userObj | Add-Member NoteProperty -Name "DeviceType" -Value $ast.DeviceType
	$userObj | Add-Member NoteProperty -Name "DeviceAccessState" -Value $ast.DeviceAccessState
	$userObj | Add-Member NoteProperty -Name "LastSuccessSync" -Value $ast.LastSuccessSync
	$report = $report += $userObj


$reportcount = $report.count

$report | Export-Csv -Path $reportfile -NoTypeInformation

}	}

Open in new window



To run this PS there are two switches:

For specific user: .\Get-ActiveSyncDeviceReport.ps1 -Identity goyalam
For All : .\Get-ActiveSyncDeviceReport.ps1 -all
Get-ActiveSyncDeviceReport.ps1.txt
McLovinAuthor Commented:
the script did not work with 2007, Any idea on how to export to csv in 2007?
Amit KumarCommented:
I thought you are running Exchange 2010 so shared it with you. Anyhow try below one:

below command have two things to do one will save output in $Results variable and then We will export $results variable to CSV file.

$results = foreach ($mbx in Get-Mailbox -ResultSize Unlimited) {Get-ActiveSyncDeviceStatistics -Mailbox $MBX.Samaccountname | Select-Object @{name="DisplayName";expression={$MBX.Name}},@{name="EmailAddress";expression={$MBX.PrimarySMTPAddress}},Devicetype,DeviceOS,LastSuccessSync,DeviceModel} 

$results | export-csv c:\activesyncdata.csv

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.