Lync ‘n Logs

I ran into an interesting quandary when working on a Lync Online deployment. If you just have the normal Active Directory install and DirSync with Office 365, using ADFS for authentication, Lync Online will use your UPN as your SIP address. Fair enough – we’d all expect this. If you’re trying to make life easy on yourself, you’re setting your UPN=Primary SMTP=SIP address. Often this is in the format of firstname.lastname@domainname.com. All this becomes very easy for users to log in and get going.

So what happens when you have something as simple as a name change? The Primary SMTP address and UPN change, but the SIP address stays the same. If you want your primary SMTP address to be different than your UPN, it gets worse. These never match. Oh, I know what you’re thinking. Who cares if your SIP address = Primary SMTP? You’re just looking the person up in Lync, anyway, so the address doesn’t matter. Sure. Until you go federate systems. Then the presence information is completely broken and communications just doesn’t happen. And don’t think you’ll be doing SIP address updates in PowerShell online, either. You’re DirSync’ing the account from your on-premises AD, so this won’t work.

So let’s fix it in the on-premises AD!

The first clue is that Lync Online will use your UPN to make a SIP address. But, if you have a SIP address on-premises, that will sync up just fine. Don’t have the Lync schema extensions in your on-premises AD? Not a problem. We are just going to add a SIP address in your list of proxy addresses from email. DirSync (or AAD Sync) will bring that right up.

Where’s the code? It’s below, and I’ll describe what’s going on here. I’m finding all the remote mailbox objects, starting in an OU. You may not want everyone, but they do need to be in Office 365 for this to work. So we pick just the remote user mailboxes.

I then look though all the proxy addresses of each user, looking for the SIP address. If I find it, I compare it to the primary SMTP address (mail attribute, really). If they are the same, I log it. If they are different, I update the SIP address and log it. If I don’t find a SIP address, I add it and log it.

This turns out to be a very “chatty” script, with all the logging going on. In the future, I may cut the logging if the primary SMTP matches the SIP address. And all this logging gave me the excuse for a good title.

#ProxyAddressFixSIP.ps1 - grb - 2/19/2015

#Adds SIP proxy address if not there, and corrects SIP address if not equal to primary SMTP.

 

#Variable Setup and Initialization

#==============================

$Date = Get-Date -Format 'yyyyMMdd-HHmm'

 

#Adjust the log path for a good logging directory

$LogPath = 'D:\provisioning\Lync\logs\ProxyAddressFixSIP-'+$Date+'.log'

Start-Transcript -Path $LogPath

 

#Change this searchbase to match your own environment

$SearchBase = 'OU=test,DC=contoso,DC=com'

 

#Main Script

#==============================

echo '----------'

echo "Getting Users"

$users = Get-ADObject -Properties DisplayName,proxyAddresses,mail,msExchRecipientTypeDetails,DistinguishedName -SearchBase $SearchBase -Filter {msExchRecipientTypeDetails -eq '2147483648'} -ResultSetSize 1000000 | sort -Property DisplayName

echo "Got Users"

 

Foreach ($User in $Users) {

 

    #Initialize variables and arrays that we'll need.

    $mail = $user.mail

    $ProxyAddresses = $User.proxyAddresses

    $NewProxyAddresses =@()

    $NewSIP = $null

 

    If ($ProxyAddresses -like 'SIP:*') {

 

        Foreach ($ProxyAddress in $ProxyAddresses) {

            If ($ProxyAddress -like "SIP:*") {

                If (("SIP:"+$mail) -eq $ProxyAddress) {

                    echo '----------'

                    echo ("SIP = MAIL "+$user.DisplayName)

                    } else {

                        echo '----------'

                        $NewSIP = "SIP:"+$mail

                        echo ("SIP Updated "+$user.DisplayName+" $NewSIP")

                        $NewProxyAddresses += $NewSIP

                        }

            } else {$NewProxyAddresses += $ProxyAddress}

            }

 

        If ($NewSIP -ne $null) {

   

            Set-ADObject $user.DistinguishedName -Replace @{proxyAddresses=$NewProxyAddresses} -Verbose

      

        }

   } else {

        echo '----------'

        $NewSIP = "SIP:"+$mail

        $NewProxyAddresses += $NewSIP

        $NewProxyAddresses += $ProxyAddresses

        echo ("SIP Added "+$user.DisplayName+" $NewSIP")

        Set-ADObject $user.DistinguishedName -Replace @{proxyAddresses=$NewProxyAddresses} -Verbose

        }

    }

 

echo '----------'

echo 'End of script'

echo '----------'

 

Stop-Transcript

 

All this code comes with the normal disclaimers, of course. Try this in a lab, if at all possible. You’re on your own here - there’s no warranty on this. At very least, try this on a test OU as the searchbase. If you use it, please leave a comment.