Thursday, September 15, 2011

Preparing your user objects in AD for Lync (phone numbers)

If you're not as lucky as I, and had the opportunity to import fresh users into your AD. You might have a lot of users, and no control over how phone numbers are stored and in which format.

Fear not. This mini-post might give you  a few clues on how to gain control of the information in AD before introducing an application such as Lync.

Why would this be a concern? You might be familiar with how to normalize phone numbers for your address book (or fail doing so). But I prefer to clean up my ad rather than trying to fix the variations in a normalization file. I often use ad for the source of many things, and I just find it easier to keep AD clean than to do fixes elsewhere.

How then? Quite easy. Powershell is your friend. Just run a get-aduser to list all of your userobject of a certain kind. Import the output to excel and challenge someone to correct the data (This is the boring and hard part, I admit). Finally, use the set-aduser to update all the objects accordingly.

Sounds easily enough? Then let's take a look at an example code:

Get-ADUser -Filter {EmailAddress -like "*"} -Properties Name, OfficePhone, MobilePhone, EmailAddress -Verbose | ft -Property Name, OfficePhone, MobilePhone, EmailAddress | Out-File c:\import\testing.csv -Width 120

Get-aduser is a great tool for this. In my example I query for users with e-mail addresses (but you can create your own filter). The query would not return all of the fields I desire, so I use the -Properties to add them to the output. By default, the output is in a FL format, which is fine if you only query one user. But hopeless if you intend to import it and use it in a spreadsheet. That is why I pipe the command to FT. But when I look at the out put, it contain a lot of information I don't really need. To control the output information  I use the -Property switch to list only what I need. Important note: It is important to add the "Name" property, as you will import information back into AD based on this ;)

Finally I pipe the entire string to the out-file command where I can control the width of the output. Default width is 80, and often not enough to display everything. You might have to adjust the width to accommodate your data.

Import the file into a spreadsheet (I use Excel), and start editing your data. Important note: Keep the headers!
When you're all set, use my previous post as a guideline and import the new values into the AD, using set-aduser cmdlet. Here's an example code:

$csvfile = read-host "file to import"
Import-Csv $csvfile | foreach {set-aduser -MobilePhone $_.MobilePhone -OfficePhone $_.OfficePhone}

These are of course not the only attributes you can edit this way. And I really encourage every sysadmin to explore the possibilities of powershell.

If you have your Lync deployment ready and you want to add users in a quick way. You might want to add a user or two manually, but the best way is to run a simple code like this (presuming everything is working on the lync server, and all users with email addresses were to have Lync):

$lyncregistrar = read-host "Front end pool to host users"
Get-ADUser -Filter {EmailAddress -like "*"} | enable-csuser -verbose -registrarpool "$lyncregistrar" -SipAddressType EmailAddress

This would enable all users with email addresses for Lync in a really quick way (Feel free to use other filters to import more granularly based on OU's or other properties you might find fit.

In my next post, I'll look on a simple script to import new users into AD, and enable them for enterprise voice at the same time. All done in a few seconds ;)

Read more about the commands I used here:
Get-ADUser, Set-ADUser, Enable-CsUser and Out-File