Powershell Bulk Load ActiveSync Device IDs into Exchange

I recently had a customer who was evaluating MDM solutions (MobileIron/Airwatch/etc) but in the interim wanted to lock down their Exchange 2010 ActiveSync to only known device IDs.

I used Paul Pozenka’s ActiveSync report script, and made a minor modification to it so it also reports back the Device IDs. See updated block below:

$EASDevices = “”| select ‘User’,’PrimarySMTPAddress’,‘DeviceID’,’DeviceType’,’DeviceModel’,’DeviceOS’, ‘LastSyncAttemptTime’,’LastSuccessSync’
$EasMailboxes = Get-Mailbox -ResultSize unlimited
foreach ($EASUser in $EasMailboxes) {
$EASDevices.user = $EASUser.displayname
$EASDevices.PrimarySMTPAddress = $EASUser.PrimarySMTPAddress.tostring()
    foreach ($EASUserDevices in Get-ActiveSyncDevice -Mailbox $EasUser.alias) {
$EASDeviceStatistics = $EASUserDevices | Get-ActiveSyncDeviceStatistics
    $EASDevices.devicetype = $EASUserDevices.devicetype
    $EASDevices.deviceid = $EASUserDevices.deviceid
    $EASDevices.devicemodel = $EASUserDevices.devicemodel
    $EASDevices.deviceos = $EASUserDevices.deviceos
$EASDevices.lastsyncattempttime = $EASDeviceStatistics.lastsyncattempttime
$EASDevices.lastsuccesssync = $EASDeviceStatistics.lastsuccesssync
    $AllEASDevices += $EASDevices | select user,primarysmtpaddress,devicetype,devicemodel,deviceid,deviceos,lastsyncattempttime,lastsuccesssync

From there I created the following simple Powershell script, which combines the devices for multiple users into a single command and then runs the Set-CASMailbox -Identity -ActiveSyncAllowedDeviceIDs accordingly.

Here it is:

$Users = Import-Csv “C:PathToFile.csv” | Sort-Object PrimarySMTPAddress
Foreach ($user in $Users)
{
if ($UserID -eq $user.PrimarySMTPAddress)
{
#Duplicate Address, so append Device ID strings.
$DeviceID = $DeviceID + “‘,'” + $user.DeviceID
$UserID = $user.PrimarySMTPAddress
}
Else
{
Set-CASMailbox -Identity $UserID -ActiveSyncAllowedDeviceIDs ‘$DeviceID’
#Single Address
$UserID = $user.PrimarySMTPAddress
$DeviceID = $user.DeviceID
}
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *


*