Tuesday, December 6, 2011

What to do when AD phone property is formatted wrong.

In my previous post, I just showed you how to search for a security group in AD, and set cs-user properties based on the selection of users.

My script is based on these users having 100% correct formatted phone attribute in Active Directory. But as some of you already know, that might not be the case. Some have formatted their phone numbers with spaces like this: +47 22 22 22 22, some might have used other characters like this +1 (454) 123 4567 and others might not have the proper E.164 international formatting and have left out their international code: (202) 123 4567.

Fear not, I will tell you how to handle this (as long as they all have the same "error" in their syntax).

If you look at the following command:

Get-ADGroup -Identity TestGroup | Get-ADGroupMember -verbose | foreach {get-csaduser -identity $_.Name | foreach {
$sipit = $_.phone
$sipit = "tel:" + $sipit
Set-CsUser -Identity $_.Name -EnterpriseVoiceEnabled $true -LineURI $sipit -PassThru | Grant-CsClientPolicy -PolicyName restrict}}

You can see we grab the "phone" attribute and populate it into a variable I have called $sipit. Then I add the "tel:" to the already existing $sipit variable. After this, the output of $sipit is "tel:-whateverwasinthephoneattrib". If the phone attrib is wrongly formatted, we need to alter this before we add the "tel:". We can do this using the -replace command. And I will sow you two examples of how to do this.

1)

Get-ADGroup -Identity $groupfetch | Get-ADGroupMember -verbose | foreach {get-csaduser -identity $_.Name | foreach {
$sipit = $_.phone
## Next line will remover anythin in the string that is not a number (including +, spaces, () and more)
$sipit = $sipit -rplace "[^0-9]"
## Next line is slightly modified to add the + back into the phone number
$sipit = "tel:+" + $sipit
Set-CsUser -Identity $_.Name -EnterpriseVoiceEnabled $true -LineURI $sipit -PassThru | Grant-CsClientPolicy -PolicyName restrict}}

Given the command above you would take a number like this +1 (234) 567 8900, or this: +1 234 5678900 and turn them into the correct lineURI format tel:+12345678900.

2)
Just as an example, I'll show how to add the national prefix if this is not present in the AD property:

Get-ADGroup -Identity $groupfetch | Get-ADGroupMember -verbose | foreach {get-csaduser -identity $_.Name | foreach {
$sipit = $_.phone
## Next line will remover anythin in the string that is not a number (including +, spaces, () and more)
$sipit = $sipit -rplace "[^0-9]"
## Next line is slightly modified to add the + and the international prefix into the phone number
## I used the North American code +1 in the example. You use whatever you need to in your deployment.
$sipit = "tel:+1" + $sipit
Set-CsUser -Identity $_.Name -EnterpriseVoiceEnabled $true -LineURI $sipit -PassThru | Grant-CsClientPolicy -PolicyName restrict}}

Quite simple, and a very quick way to enable telephony for your users. have fun labbing, and please let me know what you think about these posts.

If you need more power shell commands for Lync, I can really recommend the Lync Powershell Blog  They have a great collection of examples.