ActiveSync Reporting with PowerShell - Users with a partnership and a list of their devices

 

This script was used by a colleague to gather information for licensing purposes. Since users can have more than one device partnership (which means its a multivalued attribute), and a user (in 2010, at least) can have a maximum of 10 partnerships, it was necessary to parse through that value so that we could get a list of ALL the devices. While this bit information may not be necessary for licensing purposes, I think it's still one of those "good to know" type of things. And, I just wanted to practice parsing through the information and writing it out to a file :). 

 

Just as an FYI, once the maximum of 10 devices is reached, a user will receive an error code of STATUS_CODE_MAXIMUMDEVICESREACHED when trying to create new partnerships. They will also see a warning in OWA when viewing the "Mobile Phones" tab indicating that they have reached the maximum number of allowed partnerships, and that no new partnerships can be created until some of the others have been removed.


 

$output_file="c:\scripts\test.txt"

$users = get-casmailbox –resultSize Unlimited | where {$_.HasActiveSyncDevicePartnership -eq $true}

 

$users | foreach {$id = $_.identity; $mbstats = get-activesyncdevicestatistics -mailbox $id

 

If ($mbstats.DeviceModel -ne 1) {$devices = $mbstats.DeviceModel; $Sync = $mbstats.LastSuccessSync

        for ($i=0; $i-lt $devices.count; $i++) {"$report$($id), $($devices[$i]), $($sync[$i])" | add-content $output_file}

 

    }

    else {"$report$($id), $($mbstats.DeviceModel), $($mbstats.LastSuccessSync)" | add-content $output_file}

}

 


 

So, given the fact that each user can have a maximum of 10 partnerships, what can we do if a user reaches the maximum number of partnerships?

 

Well, once again, PowerShell to the rescue!

 


 

This bit of code will delete all ActiveSync Partnerships that are older than 7 days. Be sure to adjust the time period to a number that suits your environment! This tidbit of code is courtesy of my friend Jim Martin (also a MSFT employee).

 

#Find all of your users who have ActiveSync Partnerships:

1.) $eas = Get-CASMailbox | where { $_.HasActiveSyncDevicePartnership -eq $True } 

#Find all users within the ActiveSync Partnership list that has a “LastSyncAttempttime” less than X amount of days, here we set a week:

2.) $del = $eas | ForEach-Object { Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity | where { $_.LastSyncAttempttime -lt (Get-date).adddays(-7) }}

#Remove all ActiveSync Partnerships older than 7 days:

3.) $del | ForEach-Object { Remove-ActiveSyncDevice $_.identity -Confirm:$False }

 


Many thanks to Ethan McConnell and Jim Martin for assisting with this post.