Script: Update-MobileNumber.ps1 – Automatically Updating the Global Address List with Mobile Numbers from Exchange ActiveSync
Description
In some organizations, the Global Address List is used extensively as a phone list and corporate directory. When that’s the case, keeping the information current can be time-consuming. Users don’t always notify you of changes, and Help Desk staff have better things to do than updating stuff like that. There are applications like fellow Jim McBee’s awesome web-based Directory Update, which provides a simple interface for users to update GAL info. But that still requires that the user take the time to update the info. Here, we’ll automate the process of updating the GAL with a new mobile number when a user syncs a new ActiveSync device for the first time.
When a user synchronizes a device, information about the device is stored in Active Directory. The info can be viewed using the Get-ActiveSyncDeviceStatistics.
Get-ActiveSyncDeviceStatistics -mailbox dbingham FirstSyncTime : 10/2/2009 5:45:54 PM LastPolicyUpdateTime : 10/2/2009 5:46:38 PM LastSyncAttemptTime : 10/13/2009 4:46:38 PM LastSuccessSync : 10/13/2009 4:46:38 PM DeviceType : PocketPC DeviceID : AF053AA9D0FE3D37C5A2AC3C77ACB9F8 DeviceUserAgent : DeviceWipeSentTime : DeviceWipeRequestTime : DeviceWipeAckTime : LastPingHeartbeat : RecoveryPassword : ******** DeviceModel : RAPH800 DeviceIMEI : 0x80046B09 DeviceFriendlyName : Pocket_PC DeviceOS : Windows CE 5.2.19965 DeviceOSLanguage : English DevicePhoneNumber : 5865311234 Identity : Danielle.Bingham@mydomain.org\AirSync-PocketPC-AF053AA9D0FE3D37C5A2AC3C77ACB9F8
We see that the next-to-last field contains the device’s phone number*. So, we’ll use some code that will accomplish the following tasks:
- Get a list of all user mailboxes
- Get ActiveSync data for all devices that:
- have a phone number
- have synced for the first time in the last 24 hours
- filter out any old devices still listed (in case a user has had more than one EAS device)
- format the number in a human friendly version (hyphenate)
- Update the user’s AD account with the number
That can be accomplished using the following code:
$mailboxes = @(Get-Mailbox | ? {$_.RecipientType -eq 'UserMailbox'}) ForEach ($mailbox in $mailboxes){ $devices = @(Get-ActiveSyncDeviceStatistics -mailbox $mailbox.Alias | Where-Object {($_.DevicePhoneNumber -ne '') -and ($_.FirstSyncTime -gt (Get-Date).addhours(-24))}) | Sort-Object LastSuccessSync -descending | Select-Object -first 1 ForEach ($device in $devices){ if($device.DevicePhoneNumber){ $NumberLength = $device.DevicePhoneNumber.length if ($NumberLength -eq 10) {$DeviceNumber = $device.DevicePhoneNumber.SubString(0,3)+"-"+$device.DevicePhoneNumber.SubString(3,3)+"-"+$device.DevicePhoneNumber.SubString(6,4)} if ($NumberLength -eq 11) {$DeviceNumber = $device.DevicePhoneNumber.SubString(1,3)+"-"+$device.DevicePhoneNumber.SubString(4,3)+"-"+$device.DevicePhoneNumber.SubString(7,4)} Set-User $mailbox.Alias -MobilePhone $DeviceNumber } } }
Copy that code to notepad and save it in your scripts folder as Update-MobileNumber.ps1. Then we just run the script via a scheduled task every 24 hours. If you don’t run it every 24 hours, make sure you adjust the (Get-Date).addhours(-24) line accordingly.
* – Most devices have the number stored there. Some devices, like the Apple iPhone, unfortunately don’t.
Installation
Execution Policy: Third-party PowerShell scripts may require that the PowerShell Execution Policy be set to either AllSigned, RemoteSigned, or Unrestricted. The default is Restricted, which prevents scripts – even code signed scripts – from running. For more information about setting your Execution Policy, see Using the Set-ExecutionPolicy Cmdlet.
Donations
I’ve never been one to really solicit donations for my work. My offerings are created because *I* need to solve a problem, and once I do, it makes sense to offer the results of my work to the public. I mean, let’s face it: I can’t be the only one with that particular issue, right? Quite often, to my surprise, I’m asked why I don’t have a “donate” button so people can donate a few bucks. I’ve never really put much thought into it. But those inquiries are coming more often now, so I’m yielding to them. If you’d like to donate, you can send a few bucks via PayPal at https://www.paypal.me/PatRichard. Money collected from that will go to the costs of my website (hosting and domain names), as well as to my home lab.
Here is what I came up with for our new common area phone numbers. I am also going to add Set-CsClientPin and Set-CsClientPolicy.
Do {
$Phone=Read-Host “What is the New 10 digit Phone Number? – Example 972-830-6957 ”
} while ($Phone -match “^\d\d\d\-\d\d\d\-\d\d\d\d$” -ne “True” )
Do {
$Description=Read-Host “What is the Description ”
} while ($Description.Length -le 5 )
$LineURI= “tel:+1” + $Phone.Replace(“-“, “”)
[long]$Display=$Phone.Replace(“-“, “”)
$DisplayNumber=”{0:(###) ###-####}” -f $Display
Yeah, my script would just look for new device partnerships, and automatically update. Really a shame that MS has blocked part of the number.