# encoding: utf-8
# api: powershell
# title: Set Active Sync DeviceID
# description: Get DeviceID of all ActiveSync Devices for all Mailboxes with an ActiveSync partnership, then set the Mailboxes ActiveSyncAllowedDeviceIDs to the retrieved DeviceID.
# version: 0.1
# author: thomas torggler
# license: CC0
# x-poshcode-id: 3508
# x-derived-from-id: 3852
# x-archived: 2015-05-04T21:42:47
# x-published: 2015-07-09T11:53:00
#
#
# Requires a connection to Exchange Server, or Exchange Management Shell
$s = New-PSSession -ConfigurationName Microsoft.Exchange -Name ExchMgmt -ConnectionUri http://ex14.domain.local/PowerShell/ -Authentication Kerberos
Import-PSSession $s
# Get all Client Access Server properties for all mailboxes with an ActiveSync Device Partnership
$Mailboxes = Get-CASMailbox -Filter {HasActivesyncDevicePartnership -eq $true} -ResultSize Unlimited
# Get DeviceID for all mailboxes
$EASMailboxes = $Mailboxes | Select-Object PrimarySmtpAddress,@{N='DeviceID';E={Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity | Select-Object –ExpandProperty DeviceID}}
# Set the ActiveSyncAllowedDeviceIDs attribute of all Mailboxes
$EASMailboxes | foreach {Set-CASMailbox $_.PrimarySmtpAddress -ActiveSyncAllowedDeviceIDs $_.DeviceID}