Thursday 7 April 2016

Update Lync LineURI with Active Directory Phone Number

In this series of Blog posts I will explain how we can use the Lync Powershell Modules to help automate some Bulk Lync user tasks. While most of these task can be completed using PowerShell Remoting using the OCSPowershell provider endpoint provided by the Lync server, some of the error forwarding through the proxy doesn't work as expected. In this case we can utilise the Lync Management Shell locally on our administration console.

Mass enabling Enterprise wide options in Lync can be laborious using the Control Panel, bulk changes can be best achieved using the Lync Management Shell. The Management Shell is a PowerShell session with the Lync Modules Imported at Runtime.

This is the second post in this Series detailing some tooling to help bulk enable an organisations Lync Users.

Lets Start by Importing the Lync Module in the local session and building an Implicit Remote Session to a Domain Controller.

Import-Module Lync
$domaincreds = Get-Credential -Message "Please provide AD Creds"
$adsession = $false
$remoteSessionList = Get-PSSession -ComputerName $domaincontroller -Credential $domaincreds
Foreach($remoteSession in $remoteSessionList) {
If($remoteSession.Name -eq 'ActiveDirectory'){
$adsession = $true
break
}
}
If ($adsession -eq $false){
$dcSession = New-PSsession -Computername $domaincontroller -Name ActiveDirectory -Credential $domaincreds

# Use the newly created remote Powershell session to send a command to that session
Invoke-Command -Command {Import-Module ActiveDirectory} -Session $dcSession
# Use that session with the modules to add the available
# commandlets to your existing Powershell command shell with a
# new command name prefix.
Import-PSSession -Session $dcSession -Module ActiveDirectory -Prefix RM -AllowClobber }

We then query Lync for all Lync users where the LineURI is blank.

$CsUserNoLineURI = Get-CsUser -Filter {LineURI -eq $null}

For each of these users we get the e.164 formatted number stored in the otherTelephone attribute if it exists and set it for the Lync User after stipping out spaces and appending "tel:".

Foreach ($csuser in $CsUserNoLineURI) {
            $newLineURI, $tempuser, $updatemsg, $errormsg = $null
            $tempuser = Get-RMADUser -Identity $csuser.SamAccountName -Properties otherTelephone
            If ($tempuser.PSobject.Properties.name -match "otherTelephone" -and $tempuser.otherTelephone[0]){
                $newLineURI = "tel:"+$tempuser.otherTelephone[0] -replace " ", ""
                Try {
                    Set-CsUser -Identity $csuser.Identity -LineURI $newLineURI -ErrorAction Stop
                        #Successfully set LineURI to AD DDI 
                        $updatemsg = [pscustomobject]@{"Name" = $csuser.DisplayName; "New LineURI" = $newLineURI}
                        $LineURIUpdates += $updatemsg                 
                } Catch {
                    #Error setting DDI to LineURI
                    $errormsg = [pscustomobject]@{"Name" = $CsUser.DisplayName; "Error" = $_.Exception.message}
                    $LineURIErrors += $errormsg
                }
            }
        }

We have also added some logging into the above ForEach loop. Each time we either change something or generate an error (Using the Try Catch) we create a PSCustomObject and add it into the $LineURIErrors or $LineURIUpdates custom table to display later

Now we have our Custom tables lets send them to someone formatted in an HTML Email

 if($LineURIUpdates.Count -gt 0) {
            $LineURIbody = $LineURIUpdates |
ConvertTo-Html -Head $style -PreContent "Updated $($LineURIUpdates.Count) User LineURI fields

"
        } else {
            $LineURIbody = "No LineURI updates were applied this time"
        }
        Send-MailMessage -SmtpServer $smtpserver -From $from -to $to -Subject "Lync LineURI Updates" -BodyAsHtml -Body "$LineURIbody"

        If($LineURIErrors.Count -gt 0){
            $LineURLIErrorsBody =  $LineURIErrors |
ConvertTo-Html -Head $style -PreContent "$($LineURIErrors.count) LineURI update errors detected

"
        } else {
            $LineURLIErrorsBody = "No LineURI update errors occured this time"
        }
        Send-MailMessage -SmtpServer $smtpserver  -From $from -to $to -Subject "Lync LineURI Errors" -BodyAsHtml -Body "$LineURLIErrorsBody"

And finally clean up our AD Session we created at the beginning

Remove-PSSession -Name ActiveDirectory


Finally lets put it all together as a re-usable function with CmdLet binding.

function Update-LineURI
{
    [CmdletBinding()]
    Param
    (
        # Domain Controller to use
        [Parameter(Position=0,Mandatory=$true)][string]$domaincontroller,
        [Parameter(Position=1,Mandatory=$true)][string]$smtpserver,
        [Parameter(Position=2,Mandatory=$true)][string]$from,
        [Parameter(Position=3,Mandatory=$true)][string]$to
    )

    Begin
    {
        Import-Module Lync

        $domaincreds = Get-Credential -Message "Please provide AD Creds"
        $adsession = $false
        $remoteSessionList = Get-PSSession -ComputerName $domaincontroller -Credential $domaincreds
        Foreach($remoteSession in $remoteSessionList) {
            If($remoteSession.Name -eq 'ActiveDirectory'){
                $adsession = $true
                break
            }
        }

        If($adsession -eq $false){
            $dcSession = New-PSsession -Computername $domaincontroller -Name ActiveDirectory -Credential $domaincreds
            # Use the newly created remote Powershell session to send a #command to that session
            Invoke-Command -Command {Import-Module ActiveDirectory} -Session $dcSession
            # Use that session with the modules to add the available # commandlets to your existing Powershell command shell with a #new command name prefix.
            Import-PSSession -Session $dcSession -Module ActiveDirectory -Prefix RM -AllowClobber
        }

        $scriptpath = Split-path $script:MyInvocation.MyCommand.Path
        $style = Get-COntent "$scriptpath\htmlstyle.txt"

        $LineURIUpdates=@()
        $LineURIErrors=@()
    }
    Process
    {
        $CsUserNoLineURI = Get-CsUser -Filter {LineURI -eq $null}


        Foreach ($csuser in $CsUserNoLineURI) {
            $newLineURI, $tempuser, $updatemsg, $errormsg = $null
            $tempuser = Get-RMADUser -Identity $csuser.SamAccountName -Properties otherTelephone
            If ($tempuser.PSobject.Properties.name -match "otherTelephone" -and $tempuser.otherTelephone[0]){
                $newLineURI = "tel:"+$tempuser.otherTelephone[0] -replace " ", ""
                Try {
                    Set-CsUser -Identity $csuser.Identity -LineURI $newLineURI -ErrorAction Stop
                        #Successfully set LineURI to AD DDI 
                        $updatemsg = [pscustomobject]@{"Name" = $csuser.DisplayName; "New LineURI" = $newLineURI}
                        $LineURIUpdates += $updatemsg                 
                } Catch {
                    #Error setting DDI to LineURI
                    $errormsg = [pscustomobject]@{"Name" = $CsUser.DisplayName; "Error" = $_.Exception.message}
                    $LineURIErrors += $errormsg
                }
            }
        }


        if($LineURIUpdates.Count -gt 0) {
            $LineURIbody = $LineURIUpdates |
                ConvertTo-Html -Head $style -PreContent "Updated $($LineURIUpdates.Count) User LineURI fields

"
        } else {
            $LineURIbody = "No LineURI updates were applied this time"
        }
        Send-MailMessage -SmtpServer $smtpserver -From $from -to $to -Subject "Lync LineURI Updates" -BodyAsHtml -Body "$LineURIbody"

        If($LineURIErrors.Count -gt 0){
            $LineURLIErrorsBody =  $LineURIErrors | ConvertTo-Html -Head $style -PreContent "$($LineURIErrors.count) LineURI update errors detected

"
        } else {
            $LineURLIErrorsBody = "No LineURI update errors occured this time"
        }
        Send-MailMessage -SmtpServer $smtpserver -From $from -to $to -Subject "Lync LineURI Errors" -BodyAsHtml -Body "$LineURLIErrorsBody"

    }
    End
    {
        Remove-PSSession -Name ActiveDirectory
    }

} 
That completes this blog on updating Lync LineURI's to the Telephone Number in Active Directory. More posts in this series will follow shortly here.

No comments:

Post a Comment